如何获取表单内所有输入的id?

Jam*_*mes 17 forms arrays jquery input

如何在数组中的表单中获取输入元素的所有id?

Amb*_*ber 15

$ids = $('#myform input[id]').map(function() {
  return this.id;
}).get();
Run Code Online (Sandbox Code Playgroud)

  • +1 - `map()`是要走的路,虽然如果有`input`元素没有ID(也许是提交),你最终会在数组中输入一个空条目.您可能希望将选择器更改为:`$('#test input [id]')`,或者至少提供类似的测试:`if(this.id)返回this.id;` (5认同)

Len*_*rri 14

有点像......

<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>
Run Code Online (Sandbox Code Playgroud)


Fel*_*Als 5

您可以使用更精确的选择器缩小搜索范围:表单输入具有 id 的属性选择器

$(document).ready(function() {
    $('form input[id]').each(function() {
        formId.push(J(this).attr('id'));
});
});
Run Code Online (Sandbox Code Playgroud)