使用jQuery或javascript查找表单中的所有控件
我是jQuery的初学者。如何使用jQuery查找表单中的所有控件 我知道代码是这样的使用jQuery或javascript查找表单中的所有控件,javascript,jquery,Javascript,Jquery,我是jQuery的初学者。如何使用jQuery查找表单中的所有控件 我知道代码是这样的 function submitValidator(){ $("form :input").each(function(){ }); 我想访问他们的Id,需要应用正则表达式 部分文本框为数字,其余文本框为字母数字。有什么方法可以对它们进行排序以应用正则表达式吗?使用 function submitValidator() { $("form :input").each(function(){
function submitValidator(){
$("form :input").each(function(){ });
我想访问他们的Id,需要应用正则表达式
部分文本框为数字,其余文本框为字母数字。有什么方法可以对它们进行排序以应用正则表达式吗?使用
function submitValidator() {
$("form :input").each(function(){
var id = $(this).attr('id'); // here you got id
});
} // here missed brace
这是jQuery,不是j-query。
无论如何
您可以这样做:
$("form :input").each(function (index, element) {
var id = $(this).attr("id"); // returns the object ID
var value = $(this).val(); // returns the object value
// etc
});
$("form").find("input").each(function(){
var currentElementsId = $(this).attr("id");
if(currentElementsId.match(/^regex/)){
//do something
}
});
如果可以获取对象,则不需要获取Id
function submitValidator() {
$("form :input ,form textarea").each(function(){
$(this).yourfunction();
});
}
:input只会为您提供带有input的标记,textarea将丢失,因此也需要添加该标记我认为您需要这样做:
$("form :input").each(function (index, element) {
var id = $(this).attr("id"); // returns the object ID
var value = $(this).val(); // returns the object value
// etc
});
$("form").find("input").each(function(){
var currentElementsId = $(this).attr("id");
if(currentElementsId.match(/^regex/)){
//do something
}
});
如果您想在表单标记中获取更多的输入元素,可以在
find()
函数中放置多个选择器,如下所示find(“input,select,textarea,.className,[type='valueOfAttributeType'])
和其他任何选择器都可以在HTML中添加新的属性数据字符集
在“窗体:”之后添加要添加的所有控件:
你想用正则表达式做什么?您想只选择某些ID还是…?您想对该ID做什么regex@ArunPJohny您知道,对于文本框和文本区域,我必须检查它是数字还是字母数字。实际上,文本区域、按钮和选择都由
:input