How to get values ​​from all input elements inside a <tr> element using jQuery

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

   <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>

I also have 5 radio buttons and an input text box attached to it. They are not all on the same line, but I want to get the value of the element "pain1" and the element "cerCommentH" and display it. Please note that the code snippet is below. I have several of these elements in my form, so I cannot work with them individually, using their "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.

+5
4

, - :

$('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); 
});
+4

serialize(), :

var result = '';
$('tr input').each(function(){
    result += $(this).attr('name')+'='+$(this).val()+'&'
})
//post result after all
0
//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');
0

You can serialize it to an array, which can then be directly injected into the jQuery ajax method as data using serializeArray.

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);
0
source

All Articles