代码之家  ›  专栏  ›  技术社区  ›  spodell

jQuery向多个输入添加click函数[duplicate]

  •  0
  • spodell  · 技术社区  · 7 年前

    <table class="table">
      <tr>
         <td><input id="show-output" type="button" value=261 /></td>
         <td>mhour test202</td>
      </tr>
      <tr>
         <td><input id="show-output" type="button" value=260 /></td>
         <td>mhour test145</td>
      </tr>
    </table>
    

    功能是

    $('#show-output').click(function(e) {
      alert("hi");
    });
    

    A fiddle for this

    1 回复  |  直到 7 年前
        1
  •  1
  •   JonSnow    7 年前

    把你的id改成类,因为一个id在一个页面上应该只出现一次。

    $('.show-output').click(function(e) {
      alert("hi");
    });
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
    <table class="table">
      <tr>
         <td><input class="show-output" type="button" value=261 /></td>
         <td>mhour test202</td>
      </tr>
      <tr>
         <td><input class="show-output" type="button" value=260 /></td>
         <td>mhour test145</td>
      </tr>
    </table>