jQuery提交没有解雇 [英] jQuery submit not firing

查看:73
本文介绍了jQuery提交没有解雇的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对这个问题感到愚蠢,但为什么我的.submit没有发出警报?

I feel stupid for asking this, but why is my .submit not firing an alert?

HTML

<div class="buttonbar" style="margin-left:10%">
 <button class="btn btn-danger">Cancel</button>
 <button class="btn btn-success" id="publish">Publish</button>
</div>

JavaScript

<script type="text/javascript">
    $(document).ready(function() { 
        $('#publish').submit(function(){
            alert("hello");
        }); 
    }); 
</script>

当我点击发布时,jQuery不会弹出警报。我做错了什么?

When I click "publish" jQuery does not popup with an alert. What am I doing wrong?

推荐答案

因为它不是提交按钮,它不会有一个名为 submit 的事件,但它不在< form> 标记的范围内。

Because it is not a submit button, It wont have an event called submit while it is out of the scope of a <form> tag.

只需尝试点击事件,

$(document).ready(function() { 
    $('#publish').click(function(){
        alert("hello");
    }); 
}); 

或者您必须在html中进行更改,例如

or you have to make changes in your html like,

<div class="buttonbar" style="margin-left:10%">
 <form>
 <button class="btn btn-danger">Cancel</button>
 <input class="btn btn-success" id="publish" type="submit" value="Publish"/>
 </form>
</div>

JS:

$(document).ready(function() { 
    $('#publish').submit(function(e){
        e.preventDefault();
        alert("hello");
    }); 
});

这篇关于jQuery提交没有解雇的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆