简体   繁体   English

jQuery .click()无法正常使用多个按钮

[英]Jquery .click() not working multiple button

I want show alertbox on click button but more of button the some ID attr and work click on first button but not working click on second button. 我想在单击按钮上显示警报框,但在按钮的更多部分上显示一些ID属性,并在第一个按钮上单击工作,但在第二个按钮上不单击工作。

Jquery; jQuery;

jQuery("#submit").click(function(e){
     alert("message");
});

HTML; HTML; (Repeat html per message) (每条消息重复html)

 <div class="reply">
   <form id="vivam" method="post" onsubmit="return false;"> 
      <textarea name="reply" placeholder="Write reply here"></textarea>
      <p class="stdformbutton">
          <button id="submit" class="btn btn-primary">Reply</button> 
      </p>
   </form>
 </div>

jsfiddle Link jsfiddle链接

What is the solution to this problem? 这个问题有什么解决方案? Thank you in advance for answers. 预先感谢您的答复。

ID Must be unique . ID必须是唯一的。

Handler get attached to the first element with the id only. 处理程序仅附加到具有ID的第一个元素。

Use class instead of id . 使用class而不是id

Fiddle Demo 小提琴演示

Change HTML 变更HTML

 <button class="btn btn-primary submit">Reply</button>
//                                 ^ add class submit and remove id submit

Approach 1: IDs should be unique, use class instead of id. 方法1: ID应该是唯一的,使用类而不是ID。

HTML: HTML:

<div class="reply">
<form id="vivam" method="post" onsubmit="return false;"> 
  <textarea name="reply" placeholder="Write reply here"></textarea>
  <p class="stdformbutton">
      <button class="btn btn-primary submit">Reply</button> 
  </p>
 </form>
</div>

JS: JS:

jQuery(".submit").click(function(e){
 alert("message");
});

Working Demo Approach 1 工作演示方法1

Approach 2: You can also use attribute value selector to target element with same id. 方法2:您也可以使用属性值选择器定位具有相同ID的元素。 However this breaks the rule ids should be unique .and i do not recommend you using this: 但是,这破坏了规则ids should be unique我不建议您使用此ids should be unique

jQuery("[id=submit]").on('click',function(e){
 e.preventDefault(); 
 alert("test");
});

Working Demo Approach 2 工作演示方法2

Make it 做了

jQuery(".submitbuttons").click(function(e){

and then 接着

<button id="submit" class="btn btn-primary submitbuttons">Reply</button> 

and please give unique ids and I recommend to set the button-type attribute (button or submit) 并请提供唯一的ID,我建议设置按钮类型属性(按钮或提交)

You can use the on listener in JQuery . 您可以在JQuery使用on侦听器。 That way you listen on an class, and then do something with the id... 这样,您就可以听一堂课,然后用id做一些事情...

Here's an JSFiddle of the working code... 这是工作代码的JSFiddle ...

The HTML ( note that i've changed the ID's because ID's need to be unique, and add the class to the buttons ): HTML(请注意,我已经更改了ID,因为ID需要唯一,然后将类添加到按钮中):

<div class="reply">
    <form id="vivam_1" method="post" onsubmit="return false;">
        <textarea name="reply" placeholder="Write reply here"></textarea>
        <p class="stdformbutton">
            <button id="button_1" class="btn btn-primary submit">Reply</button>
        </p>
    </form>
</div>
<div class="reply">
    <form id="vivam_2" method="post" onsubmit="return false;">
        <textarea name="reply" placeholder="Write reply here"></textarea>
        <p class="stdformbutton">
            <button id="button_2" class="btn btn-primary submit">Reply</button>
        </p>
    </form>
</div>

And the JQuery part: 和JQuery部分:

$('body').on('click', '.submit', function () {
    e.preventDefault();
    alert("Clicked on button " + $(this).attr('id'));
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM