您好,我有很多具有这种结构的行:
<div class="row">
<div class="input-group col-lg-1" >
<span class="input-group-addon">
<input type="checkbox">
</span>
<span id="value">
@Html.DisplayFor(modelitem => item.DeviceInstanceId)
</span>
</div>
*
*
*
</div>
其中 @Html.DisplayFor(modelitem => item.DeviceInstanceId)
是一个数字。
我得到与选中的复选框连接的跨度值。 使用此代码:
$(document).ready(function () {
$("#reservations").click(function () {
var Ids = new Array();
$(".table input:checked").each(function () {
Ids.push(($(this).parentsUntil("row").children("#value").text()));
})
console.log(Ids);
});
});
ID 看起来像这样:
["↵ 38↵ ", "↵ 40↵ ", "↵ 41↵ "]
我应该如何改进我的 JS 代码以消除这些输入和空格?
最佳答案
您可以:
-
Ids.push($.trim($(this).parentsUntil("row").children("#value").text()));
删除前导和尾随空白字符,或
编写 HTML 时不要包含换行符和空格:
<span id="value">@Html.DisplayFor(modelitem => item.DeviceInstanceId)</span>
关于javascript - jQuery text() 返回不需要的字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21454575/