【发布时间】:2014-08-02 16:30:55
【问题描述】:
以下代码将来自 html 表的选中复选框的行信息存储到一个数组中:
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js" type="text/javascript"></script>
<script>
function myFunc() {
var myValues = new Array();
$.each($("input[name='selection[]']:checked").closest("td").siblings("td"),
function () {
myValues.push($(this).text());
});
alert("values: " + myValues.join(" "));
}
</script>
<table border="2">
<tr>
<th>Select</th>
<th>ID</th>
<th>NAME</th>
<th>JOB</th>
</tr>
<tr>
<td align="center"><input type="checkbox" class="selection" name="selection[]"></td>
<td>11</td>
<td>John</td>
<td>Doctor</td>
</tr>
<tr>
<td align="center"><input type="checkbox" class="selection" name="selection[]"></td>
<td>102</td>
<td>Anne</td>
<td>Pilot</td>
</tr>
<tr>
<td align="center"><input type="checkbox" class="selection" name="selection[]"></td>
<td>203</td>
<td>Laura</td>
<td>Teacher</td>
</tr>
</tr>
</table>
<input type="button" value="get it" onclick="javascript:myFunc(); return false;">
现在我需要将选定的 id 放在逗号分隔的字符串中(如果选中的所有复选框都是:str1="11,102,203";)
ID 加上另一个字符串中所选行中的其余信息(如果选中的所有复选框都是:str2 = "11, John, Doctor,102, Anne,Pilot, 203, Laura, Teacher";)。
我不知道该怎么做。我正在考虑将键添加到 myValues 数组,但我不知道这是否是一个好方法(并且不知道如何去做.. 对 jquery 来说太新了:()
【问题讨论】:
-
使用 fn.map 并加入它api.jquery.com/map fn.map 的文档包含您正在寻找的示例 btw
-
提供有关您希望 2 个字符串的外观的更多信息,要求不明确。 Id 的字符串应该看起来像 selectedIdsStr ="11,203"。另一个字符串将如何看起来像 otherInfoStr="11 John Doctor,203 Laura Teacher"?
-
你是对的。编辑了我的帖子。谢谢,桑吉夫
标签: jquery arrays checkbox html-table row