javascript - jQuery text() 返回不需要的字符

标签 javascript jquery

您好,我有很多具有这种结构的行:

 <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 代码以消除这些输入和空格?

最佳答案

您可以:

  • Trim the value :

    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/

相关文章:

jquery - JsPlumb: Bootstrap 导航栏填充后错误的 anchor 位置

javascript - 如何在挂载事件中为vue组件数据成员赋值

javascript - 如何在 angularjs 中单击时调用 $scope.close Popup?

javascript - 获取从今天开始的星期几

jquery - 将名称属性添加到生成的 <span>

javascript - jQuery:为什么我的脚本无法删除该类?

jQuery 链接与下载?

jQuery nestedsortable插件默认添加css

javascript - 我似乎无法使这个 if 语句在 Javascript 中工作 :S

javascript - 在更改时在两个下拉菜单之间传递值。无需点击即可更新