我有一个包含动态生成的评论表单的页面,我正在使用jQuery的$("form").submit(function() {
来提交表单。
它在评论系统上运行得很好,但它也抓住了页面上的其他表单,例如搜索表单,我想执行默认操作,将用户发送到搜索页面,搜索结果。
如何保留此脚本但更改它以便$("form").submit
函数不会抓取搜索表单?
答案 0 :(得分:0)
你提到的选择器:
$("form")
将在页面上选择所有表单元素。您需要做的是使用更具体的选择器,如id或类。你提到你有多个评论表,所以可能是这样的:
<form class="commentform"></form>
<form class="commentform"></form>
<form id="searchform"></form>
<script>
$("form.commentform").submit(function() {
// the same handler will apply to all commentform forms, but
// the keyword 'this' will reference the particular form that
// triggered the event
});
$("#searchform").submit(function() { });
</script>
(搜索表单的位置我给出了一个通过id选择的例子。)
进一步阅读: