最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - jQuery Select All Textboxes In Each Row Where Checkbox is Checked - Stack Overflow

programmeradmin3浏览0评论

I have view that contains a table with the following structure:

<table id="mappings">
    <thead>
        <tr>
            <th width="40%"><input type="checkbox" id="cb-master" /> Structure</th>
            <th width="15%">Excel Column</th>
            <th width="15%">Excel Row Start</th>
            <th width="15%">Excel Row End</th>
        </tr>
    </thead>
    <tbody>
        <tr>
            <td><input type="checkbox" id="cb-1" value="1" /> Item 1</td>
            <td><input type="text" id="col-1" value="A" /></td>
            <td><input type="text" id="rstart-1" value="5" /></td>
            <td><input type="text" id="rend-1" value="20" /></td>
        </tr>
        <tr>
            <td><input type="checkbox" id="cb-2" value="2" /> Item 2</td>
            <td><input type="text" id="col-2" value="B" /></td>
            <td><input type="text" id="rstart-2" value="5" /></td>
            <td><input type="text" id="rend-2" value="20" /></td>
        </tr>
          .
          .
          .
        <tr>
            <td><input type="checkbox" id="cb-n" value="n" /> Item n</td>
            <td><input type="text" id="col-n" value="Z" /></td>
            <td><input type="text" id="rstart-n" value="5" /></td>
            <td><input type="text" id="rend-n" value="20" /></td>
        </tr>
    </tbody>
</table>

Where ItemId is the value contained in the checkbox, and the id of each input is appended with the Id of the current item. How can I iterate through the table above with jQuery and create a JSON array containing the values of the textboxes in each row where the checkboxes checked value is set to true?

I would like the JSON object to look like this (if possible):

[
    { "ItemId": "1", "ExcelColumn": "A", "ExcelRowStart": "5", "ExcelRowEnd": "20" },
    { "ItemId": "2", "ExcelColumn": "B", "ExcelRowStart": "5", "ExcelRowEnd": "20" },
        .
        .
        .
    { "ItemId": "n", "ExcelColumn": "Z", "ExcelRowStart": "5", "ExcelRowEnd": "20" }
]

I have view that contains a table with the following structure:

<table id="mappings">
    <thead>
        <tr>
            <th width="40%"><input type="checkbox" id="cb-master" /> Structure</th>
            <th width="15%">Excel Column</th>
            <th width="15%">Excel Row Start</th>
            <th width="15%">Excel Row End</th>
        </tr>
    </thead>
    <tbody>
        <tr>
            <td><input type="checkbox" id="cb-1" value="1" /> Item 1</td>
            <td><input type="text" id="col-1" value="A" /></td>
            <td><input type="text" id="rstart-1" value="5" /></td>
            <td><input type="text" id="rend-1" value="20" /></td>
        </tr>
        <tr>
            <td><input type="checkbox" id="cb-2" value="2" /> Item 2</td>
            <td><input type="text" id="col-2" value="B" /></td>
            <td><input type="text" id="rstart-2" value="5" /></td>
            <td><input type="text" id="rend-2" value="20" /></td>
        </tr>
          .
          .
          .
        <tr>
            <td><input type="checkbox" id="cb-n" value="n" /> Item n</td>
            <td><input type="text" id="col-n" value="Z" /></td>
            <td><input type="text" id="rstart-n" value="5" /></td>
            <td><input type="text" id="rend-n" value="20" /></td>
        </tr>
    </tbody>
</table>

Where ItemId is the value contained in the checkbox, and the id of each input is appended with the Id of the current item. How can I iterate through the table above with jQuery and create a JSON array containing the values of the textboxes in each row where the checkboxes checked value is set to true?

I would like the JSON object to look like this (if possible):

[
    { "ItemId": "1", "ExcelColumn": "A", "ExcelRowStart": "5", "ExcelRowEnd": "20" },
    { "ItemId": "2", "ExcelColumn": "B", "ExcelRowStart": "5", "ExcelRowEnd": "20" },
        .
        .
        .
    { "ItemId": "n", "ExcelColumn": "Z", "ExcelRowStart": "5", "ExcelRowEnd": "20" }
]
Share Improve this question edited Nov 17, 2011 at 16:18 shuniar asked Nov 17, 2011 at 16:03 shuniarshuniar 2,6226 gold badges32 silver badges38 bronze badges 1
  • There is no such thing as a "JSON Object". Do you want a javascript object or a String containing the JSON representation of that object? – James Montagne Commented Nov 17, 2011 at 16:15
Add a ment  | 

3 Answers 3

Reset to default 7

Here you go:

$("#mappings tr input:checkbox:checked")
    .closest("tr").find("input:text").css({ "border" : "1px solid red" });

Here's a jsFiddle: http://jsfiddle/xgSpp/

To serialize the selection to JSON you can simply call serializeArray. The only catch is that you have to provide names for the elements.

var result = $("#mappings tr input:checkbox:checked").closest("tr").find("input:text").serializeArray();
alert(JSON.stringify(result));

If you add class for the input, then coding can be simpler

 <tr>
        <td><input type="checkbox" id="cb-1" value="1" class="ItemId" /> Item 1</td>
        <td><input type="text" id="col-1" value="A" class="ExcelColumn"/></td>
        <td><input type="text" id="rstart-1" value="5" class="ExcelRowStart" /></td>
        <td><input type="text" id="rend-1" value="20" class="ExcelRowEnd" /></td>
 </tr>

var result = [];
$("tbody :checked").each(function(){
   var tr = $(this).parent('td').parent('tr');
   var item = {};
   $("input", tr).each(function(){
       item[$(this).attr("class")] = $(this).val();
   });
   result.push(item);
});

check this at http://jsfiddle/mxELP/4/

Alright so here's what I came up with: http://jsfiddle/manuel/rmwSY/

Mind that I think you made a typo on <td><input type="checkbox" id="cb-2" value="1" />. Shouldn't this be value="2"?

发布评论

评论列表(0)

  1. 暂无评论