0

嘿所有我一直在寻找一段时间,并没有太多运气找到这背后的原因,所以希望有人可以帮助我。我不明白为什么它返回换行符

$('#tbl tr td').click(function () {

// Here selection ="           710-610A      "  spaces are included 
             var selection = $(this).text();
             alert(selection)

            $.ajax({                     
                    type: "Post",
                    url: "/Home/Index/",
                    dataType: "json",
                    data: {  grpCode: selection },
                    success: function (ResponseData) {},
                    error: function (errorResponse) {
                    alert('AJAX Call Failed');
                }

 // When passed grpCode = "\n                  710-610A\n          "
   I dnt understand why theres so many spaces along with it 
    [HttpPost]
    public ActionResult Index(string grpCode)
    {                    
        // do something
    }

我认为这与我在视图中显示表格的方式有关

 <table id="tbl">
    <tr>
        <th>
            Groups
        </th>
    </tr>

 @foreach (var item in Model.groupCodesList.Select(m=> m.Group_Code).Distinct())
  {
      <tr>
          <td>
              @item
           </td>
      </tr>
  }
</table>

我正在使用 EF Code First 来检索数据

4

2 回答 2

0

的内容

          <td>
              @item
          </td>

"
              @item
          "

(作为多行字符串文字)

通常,在 HTML 中,空格主要被忽略,但它们仍然是内容的一部分。它们必须是,您可以使用 CSSwhite-space使它们可见(如<pre>)。因此,当您使用 JQuery 时.text(),您将获得完整的内容,包括看似多余的空白。

如果您首先确保没有空格,

<td>@item</td>

不会插入额外的空格。

于 2013-09-27T21:04:52.043 回答
0

我通过在文本中添加 .trim() 解决了这个问题

$('#tbl tr td').click(function () {
         var selection = $(this).text().trim();
         alert(selection)

有谁知道为什么这是一个问题?

于 2013-09-27T20:54:03.950 回答