不使用页面中存在的javascript提交特定表单

submit specific form without use the javascript exist in page

本文关键字:提交 javascript 表单 存在      更新时间:2023-09-26

我有很多表单,每次点击都要提交一个表单。我使用这个代码。

$(document).ready(function(){
    $("form").submit(function(){ 
        var str = $(this).serialize();
        $.ajax({
            type: "POST",
            url:"function.php",
            data: $(this).serialize(),
            success: function(html) {
                alert(html);
                window.location.reload();
            }
        });
        return false;
    });
});

我所有的表格都是这样的

<form method='post' id='title' name='title'>
  <input type='hidden' name='test' value='test' >
    <button type='submit' name='submit'></button>
</form>

我的剧本写得很好--直到--

我在页面中添加了一个不同的表单。对于这个表单,我不想使用javascript。当我点击这个不同的表单时,javascript就会出现!

有什么帮助吗?

给表单一个唯一的ID,那么只有您想要提交的表单才会使用您想要使用的javascript提交。例如

$(document).ready(function(){
    $("form#title").submit(function(){ /* <-- notice form#title */
        var str = $(this).serialize();
        $.ajax({
            type: "POST",
            url:"function.php",
            data: $(this).serialize(),
            success: function(html) {
                alert(html);
                window.location.reload();
            }
        });
        return false;
    });
});
<!-- this form will attach to above JS -->
<form method='post' id='title' name='title'>
  <input type='hidden' name='test' value='test' >
    <button type='submit' name='submit'></button>
</form>