简体   繁体   English

jQuery单击事件未在表单内的按钮上触发

[英]jQuery click event not triggered on button inside form

I have a button inside a form like this: 我在这样的表单中有一个按钮:

<form>
   <input type="text" />
   <input type="password" />
   <button type="button" class="btn-login">Log in</button>
 </form>

I am trying to trigger the button's click event by doing this in the js: 我试图通过在js中执行此操作来触发按钮的单击事件:

$(document).ready(function () {
     $('.btn-login').live("click", function () {
          alert("Button clicked!");
      });
});

But for some reason, this is not working. 但由于某种原因,这是行不通的。

If I remove the <form></form> that is around the textboxes and button, the click event is triggered, but this also means that my page is not showing properly, so I wonder if there is a way to trigger the button's click event when inside the form. 如果我删除文本框和按钮周围的<form></form> ,则触发click事件,但这也意味着我的页面显示不正确,所以我想知道是否有办法触发按钮的点击表格内部的事件。

If you are using latest version of Jquery the try on instead of live as it is deprecated in version 1.9. 如果您正在使用最新版本的jQuery的尝试on ,而不是live ,因为它是在1.9版本弃用。 If you are using version 1.7 then your code will work. 如果您使用的是1.7版本,那么您的代码将起作用。

$(document).ready(function () {
     $('.btn-login').on("click", function () {
          alert("Button clicked!");
      });
});

JS Fiddle .on() example JS Fiddle .on()示例

JS Fiddle .live() example JS Fiddle .live()示例

.live Function() has been removed as from jQuery 1.9 . .live Function()已从jQuery 1.9中删除。 Reference: http://api.jquery.com/live/ 参考: http//api.jquery.com/live/

Please use .on() function for binding events. 请使用.on()函数进行绑定事件。 Reference: http://api.jquery.com/on/ 参考: http//api.jquery.com/on/

Solution: 解:

$(document).ready(function () {
     $('.btn-login').on("click", function () {
          alert("Button clicked!");
      });
});

My guess is that you are using a newer version of jquery, I tested with 1.9.1 and jQuery.live does not work (removed jquery 1.9). 我的猜测是你使用的是更新版本的jquery,我测试了1.9.1并且jQuery.live不起作用(删除了jquery 1.9)。 A simple change to jQuery.on and it sprang immediately to life. jQuery.on进行了简单的改动,它立即栩栩如生。

<form>
    <input type="text" />
    <input type="password" />
    <button type="button" class="btn-login">Log in</button>
</form>

$(document).ready(function () {
    $('.btn-login').on("click", function () {
        alert("Button clicked!");
    });
});

on jsfiddle jsfiddle

As a note to comments, this is what the W3C has to say 作为评论的注释, 这就是W3C所说的

Buttons created with the BUTTON element function just like buttons created with the INPUT element, but they offer richer rendering possibilities: the BUTTON element may have content. 使用BUTTON元素创建的按钮功能就像使用INPUT元素创建的按钮一样,但它们提供了更丰富的渲染可能性:BUTTON元素可能包含内容。 For example, a BUTTON element that contains an image functions like and may resemble an INPUT element whose type is set to “image”, but the BUTTON element type allows content. 例如,包含图像的BUTTON元素的功能类似于并且可能类似于其类型设置为“image”的INPUT元素,但BUTTON元素类型允许内容。

The Button Element - W3C 按钮元素 - W3C

This article demonstrates some of the differences between button and input 本文演示了按钮和输入之间的一些区别

And what of W3cschools advice? W3cschools的建议是什么?

Tips and Notes Note: If you use the element in an HTML form, different browsers may submit different values. 提示和注释注意:如果您在HTML表单中使用该元素,则不同的浏览器可能会提交不同的值。 Use to create buttons in an HTML form. 用于在HTML表单中创建按钮。

W3cschools: HTML Tag W3cschools:HTML标签

w3cschools are not an authorative body. w3cschools不是一个授权机构。 As far as I can tell, the only browsers that had real issues were IE6 & IE7, so I guess their advice is a little out of date. 据我所知,唯一有真正问题的浏览器是IE6和IE7,所以我猜他们的建议有点过时了。 It is possible that there are others but I could not find anything concrete. 有可能还有其他但我找不到任何具体的东西。 the best I could find was on MDN <\\button> 我能找到的最好的是MDN <\\ button>

IE7 has a bug where when submitting a form with Click me, the POST data sent will result in myButton=Click me instead of myButton=foo. IE7有一个错误,当用Click Click提交表单时,发送的POST数据将导致myButton = Click me而不是myButton = foo。 IE6 has an even worse bug where submitting a form through a button will submit ALL buttons of the form, with the same bug as IE7 This bug has been fixed in IE8 IE6有一个更糟糕的错误,通过按钮提交表单将提交表单的所有按钮,与IE7相同的错误此错误已在IE8中修复

I think below code should work for you 我认为下面的代码应该适合你

$(document).ready(function () {
     $(document).delegate('.btn-login', "click", function () {
          alert("Button clicked!");
      });
});

It uses delegate from jquery, so even if it is added later in DOM via ajax, this function would work. 它使用来自jquery的委托,所以即使稍后在DOM中通过ajax添加它,这个函数也可以工作。

You can migrate 1.1.0 to 1.9.1 to use .live() 您可以将1.1.0迁移到1.9.1以使用.live()

$(document).ready(function () {
     $('.btn-login').live("click", function () {
          alert("Button clicked!");
      });
});

[Working Example] ( http://jsfiddle.net/3hQbG/ ) [工作实例]http://jsfiddle.net/3hQbG/

instead of 代替

<button type="button" class="btn-login">Log in</button> 

use 采用

<input type="button" class="btn-login">Log in</input>

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

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