如何获得"动态id"在jQuery?

时间:2015-10-21 08:23:13

标签: jquery html razor

如何获得"动态id"在jQuery?

例如:Razor代码:

@model ...
...
@{
   int i = 0;
   foreach (var item in Model)
   {
      i = i + 1;
      <tr>
         <td class="check">
            <div tabindex="0" id="a[@i]">print</div>    
      </td>
   </tr>
   }
}

<script type="text/javascript">
    $("#a[1]").keyup(function (e) {
        if (e.keyCode === 13) { // enter == 13
            $("#a[2]").focus();
        }
    });
</script>

&#34; #a的[1]&#34;&#34; #a的[2]&#34; - 不行,我该怎么办?

1 个答案:

答案 0 :(得分:1)

像其他人说的那样,为所有共享元素放置公共类,或者只是将它与此绑定:

// bind only id with a in first words
// but this will problem if exist other element use `a` in front of
$("div[id^=a]").keyup(function (e) {
    if (e.keyCode === 13) { // enter == 13
        hideListShowImg();
    }
});

<强>更新

$("#a[1]") /* should be ----> */ $("#a\\[1\\]")