2013-07-25 22 views
5

我是jQuery的入门者。如何使用jQuery查找表单中的所有控件?使用jQuery或javascript查找表单中的所有控件

我知道代码是这样的

function submitValidator(){ 
    $("form :input").each(function(){ }); 

我想访问他们的ID和需要办理的正则表达式

一些文本框是数字其余将是字母数字。有没有什么方法可以将它们排序以应用正则表达式?

+1

你想用正则表达式做什么?你想只选择某些ID或...? – pythonian29033

+0

你想要做什么与正则表达式 –

+0

@ArunPJohny你知道的文本框和文本区域我必须检查它是数字或字母数字。 –

回答

6

您可以在HTML中添加新的属性数据的字符集

<input type="text" id='amt' data-charSet='numeric'>

您希望以后要添加的所有controlles加“的形式:”

function submitValidator(){ 
        $("form :text, textarea").each(function(){   
         var NumericOnly = "^[0-9]*$"; 
         var svId = $(this).attr('id'); 
      if($(this).attr('data-charSet') == 'numericonly') 
        { 
         if(!$(this).val().match(NumericOnly)) { 
          alert("numeric"); 
          eval("$('#" + svId +"').focus();") 
          return false; 
          } 
        } 
        }); 
      } 
1

使用

function submitValidator() { 
    $("form :input").each(function(){ 
     var id = $(this).attr('id'); // here you got id 
    }); 
} // here missed brace 
2

这是jQuery的,不J-查询。 反正...

你可以这样说:

$("form :input").each(function (index, element) { 
    var id = $(this).attr("id"); // returns the object ID 
    var value = $(this).val(); // returns the object value 
    // etc 
}); 
0

你不需要得到ID如果能得到对象

function submitValidator() { 
    $("form :input ,form textarea").each(function(){ 
    $(this).yourfunction(); 
    }); 
} 

:与输入输入只会给你的标签,textarea将错过所以需要补充的,以及

+0

实际上textarea,button和select都是通过':input' –

0

我认为你想要做这样的事情:

$("form").find("input").each(function(){ 
    var currentElementsId = $(this).attr("id"); 
    if(currentElementsId.match(/^regex/)){ 
     //do something 
    } 
}); 

如果你想在表单标签中获得比输入元素更多的内容,你可以在find()函数中放置多个选择器,像这样; find("input,select,textarea,.className,[type='valueOfAttributeType']"),显然任何其他选择

相关问题