jQuery 捕获提交按钮jquery的点击事件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12356794/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
capture click event of submit button jquery
提问by SoftwareSavant
I am trying to get the click event of a submit button on my form...
我正在尝试获取表单上提交按钮的点击事件...
<input type="submit" value="Search By Demographics" class="button" id="submitDemo"/>
That is the button that I want to get the event from.
那是我想从中获取事件的按钮。
$('#submitDemo').click(
alert("work darn it!!!!!!!!!!!!!!!!!!!!!!!!!")
//$("#list").block({ message: '<img src="../../Images/ajax-loader.gif" />' })
);
This is what I want to do on button click. I want to put a loading image around a div and then unblock later in a different function. Currently the overlay pops up when the page loads instead of onClick. What am I doing
这就是我想要在按钮单击时执行的操作。我想在 div 周围放置一个加载图像,然后在不同的函数中解除阻塞。当前,当页面加载而不是 onClick 时会弹出覆盖层。我在做什么
回答by NullPoiиteя
try this
尝试这个
$('#submitDemo').live("click",function() {
// your stuff
});
As @px5x2 pointed that live is deprecate so instead of live use ON
正如@px5x2 指出 live 已弃用,因此而不是 live 使用 ON
$('#submitDemo').on("click",function() {
// your stuff
});
回答by Chuck Callebs
$('#submitDemo').click(function() {
// do your stuff
});
回答by xandercoded
You need to run your script when the DOM is ready, as well as providing a callback to the click method:
您需要在 DOM 准备好时运行您的脚本,并提供对 click 方法的回调:
$(function() {
$('#submitDemo').click(function() {
// do your stuff
});
});
回答by Sushanth --
You have to encase your code in a function..
你必须将你的代码封装在一个函数中..
$('#submitDemo').click(function(){
alert("work darn it!!!!!!!!!!!!!!!!!!!!!!!!!")
//$("#list").block({ message: '<img src="../../Images/ajax-loader.gif" />' })
});
But it is always a good practice to bind the event using on so that it attaches the event handler to the element when that is available.. So
但是使用 on 绑定事件始终是一个好习惯,以便在可用时将事件处理程序附加到元素上。所以
$('#submitDemo').on('click',function(){
alert("work darn it!!!!!!!!!!!!!!!!!!!!!!!!!")
//$("#list").block({ message: '<img src="../../Images/ajax-loader.gif" />' })
});
回答by Paritosh Singh
For form you can also bind submit button click on form.
对于表单,您还可以绑定提交按钮单击表单。
$("#form_selector").submit(function{
// Do your stuff
})
where 'form_selector' is id of the form of which submit button is clicked.
其中 'form_selector' 是点击提交按钮的表单的 id。
回答by Bariock
You might haven't included the jquery library in your code!
您可能没有在代码中包含 jquery 库!
src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>