javascript 如何使用 JQuery 从 <tr> 元素内的所有输入元素获取值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10998806/
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-10-26 11:46:02  来源:igfitidea点击:

How to get the values from all input elements inside a <tr> element using JQuery

javascriptjqueryhtml

提问by Sapphire

How do I get all the input elements in one row in my form? For example, in the code snippet below, I have a checkbox and a text input box. I want to get the values of both these input types and display them to the user in the next td element containing the div id="hist" element.

如何在表单中的一行中获取所有输入元素?例如,在下面的代码片段中,我有一个复选框和一个文本输入框。我想获取这两种输入类型的值,并在包含 div id="hist" 元素的下一个 td 元素中向用户显示它们。

   <tr><td>Head</td>
            <td><input type="text" name="headH" id="headH" ></td>
            <td><input class="NA" type="checkbox" name="headNA" id="headNA" value="N/A"></td>
            <td><div class="hist"></div><%=Utils.getMeasurementsCreateDiv1("Head","H",num) %>
            </td></tr>
        <tr><td>Neck</td>
            <td><input type="text" name="neckH" id="neckH" ></td>
            <td><input class="NA" type="checkbox" name="neckNA" id="neckNA" value="N/A"></td>
            <td><div class="hist"></div><%=Utils.getMeasurementsCreateDiv1("Neck","H",num) %></td>
        </tr>
        <tr><td>UE</td>
            <td><input type="text"name="uEH" id="uEH"></td>
            <td><input class="NA" type="checkbox" name="ueNA" id="ueNA" value="N/A"></td>
            <td><div class="hist"></div><%=Utils.getMeasurementsCreateDiv1("UExt","H",num) %></td>

Also I have a 5 radio buttons and a text input box attached to it. These are all not on the same row but I want to get the value of the "pain1" element and the "cerCommentH" element and display it. Please note that the below is just a code snippet. I have several such elements in my form so I can't work with them individually using their "id".

此外,我有一个 5 个单选按钮和一个附加到它的文本输入框。这些都不在同一行,但我想获取“pain1”元素和“cerCommentH”元素的值并显示它。请注意,以下只是一个代码片段。我的表单中有几个这样的元素,所以我无法使用它们的“id”单独使用它们。

   <tr>
            <td>Mech</td>
            <td>
                <input type="radio" name="pain1" value="Pain a">Pain a
                <input type="radio" name="pain1" value="Pain b">Pain b

            </td></tr>  
            <tr><td></td>
            <td>
                <input type="radio" name="pain1" value="Pain c">Pain c  
                <input type="radio" name="pain1" value="Pain d">Pain d
                <input type="radio" name="pain1" style="display:none;" value="">
                <input type="button" value="Clear" onclick="document.history.pain1l[4].checked = true;">
        </td>
        <td><div class="hist"></div><%=Utils.getMeasurementsCreateDiv1("Cehi","H",num) %></td>
        </tr>
        <tr>
            <td></td>
            <td><input type="radio" name="pain1" value="Other">Other
                <input type="text" name="cerCommentH" id="cerCommentH">
            </td>
            <td></td>
            <td></td>
        </tr>

Thanks in advance for your help.

在此先感谢您的帮助。

采纳答案by lucuma

Assuming perhaps you want to do something with each one:

假设您可能想对每个人做一些事情:

$('tr:has(input)').each(function() {
   var row = this;
   var values = "";
   $('input', this).each(function() {
      values =  values + "," + $(this).val() 
   });
   values = "<div>(" + values.substring(1) + ")</div>";

   $('.hist', row).html(values); 
});

回答by Kevin B

You can serialize it into an array that can then be directly input into a jquery ajax method as data using serializeArray.

您可以将其序列化为一个数组,然后可以使用 serializeArray 将其作为数据直接输入到 jquery ajax 方法中。

var rowIndex = 1; // row index to pull
// select all inputs that you desire to send, then use serializeArray();
var data = $("tr").eq(rowIndex).find("input").serializeArray();
$.post(url,data,onDoneHandler);

回答by Sergei Zahharenko

If you need it serialize() way then:

如果你需要它 serialize() 方式,那么:

var result = '';
$('tr input').each(function(){
    result += $(this).attr('name')+'='+$(this).val()+'&'
})
//post result after all

回答by Jerry

//suppose trid is the id of the tr element
var all = $('#tr input');
//or if tr has no id, but table does have a id 'table1'
var tr = $('input', '#table1 tr:first');