jQuery 如何获取表单内所有输入的id?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2981419/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 14:40:51 来源:igfitidea点击:
How to Get id's of all inputs inside the form?
提问by James
How to get all id's of input elements inside a form in an array?
如何获取数组中表单内输入元素的所有ID?
采纳答案by Leniel Maccaferri
Something along the lines...
沿线的东西......
<script src="../../Scripts/jquery-1.4.2.min.js"></script>
<script type="text/javascript">
$(document).ready(function ()
{
// Get all the inputs into an array...
var $inputs = $('#myForm :input');
// An array of just the ids...
var ids = {};
$inputs.each(function (index)
{
// For debugging purposes...
alert(index + ': ' + $(this).attr('id'));
ids[$(this).attr('name')] = $(this).attr('id');
});
});
</script>
回答by Amber
$ids = $('#myform input[id]').map(function() {
return this.id;
}).get();
回答by FelipeAls
You can narrow your search with a more precise selector : form input andan attribute selector for the ones having an id
您可以使用更精确的选择器缩小搜索范围:表单输入和具有 id 的属性选择器
$(document).ready(function() {
$('form input[id]').each(function() {
formId.push(J(this).attr('id'));
});
});