表单提交上的jQuery绑定无法正常工作 [英] jQuery bind on form submit is not working
问题描述
我有一个这样的表格:
<form class="category_form" id="category_form" method="post">
<p class="ui-widget">
<label for="birds">Birds: </label>
<input id="birds" /> <a id="add_category" href="#">Add Category</a>
</p>
<p class="ui-widget" style="margin-top:2em; font-family:Arial">
Result:
<textarea id="log" style="height: 200px; width: 300px; overflow: auto;" class="ui-widget-content"></textarea>
</p>
<p>
<input type="submit" class="button" value="Add">
</p>
</form>
我有一个像这样的jQuery绑定函数:
and I have a jQuery bind function like this:
$('#category_form').bind('submit',function()
{
// Get the variables
alert ("hello");
return false;
});
我想要发生的是每次按下提交按钮,jQuery功能都会触发,警报会发生,但由于某种原因它不能正常工作。
What I wanted to have happen is every time submit button is pressed, the jQuery function would fire and the alert would happen, but for some reason it isn't working as I thought it would.
实例是在这个测试页面上: http://problemio.com/test.php
The live example is on this test page: http://problemio.com/test.php
任何想法为什么要按添加按钮不能使上面的jQuery工作吗?
Any idea why pressing the add button does not make the above jQuery work?
谢谢!!
推荐答案
您的绑定应该包含在 $(document).ready
中,或者它尝试在表单存在于页面之前绑定表单:
your binding should be wrapped in $(document).ready
or it attempts to bind the form before the form exists on the page:
$(document).ready(function(){
$('#category_form').bind('submit',function(){
...
});
});
这篇关于表单提交上的jQuery绑定无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!