使用 Ajax 提交表单两次

我尝试使用 Ajax 提交表单,但它提交了输入的值两次。您能帮我防止这个问题吗?我尝试了之前问题中的一些建议,但没有成功。


谢谢


网页代码:


<form id="ratingForm" method="POST">

    <div class="form-group">

        <h4>Rate this product</h4>

        <!-- Start Star -->

        <button type="button" class="btn btn-warning btn-sm rateButton" aria-label="Left Align">

          <span class="glyphicon glyphicon-star" aria-hidden="true"></span>

        </button>

        

        <button type="button" class="btn btn-default btn-grey btn-sm rateButton" aria-label="Left Align">

          <span class="glyphicon glyphicon-star" aria-hidden="true"></span>

        </button>

        

        <button type="button" class="btn btn-default btn-grey btn-sm rateButton" aria-label="Left Align">

          <span class="glyphicon glyphicon-star" aria-hidden="true"></span>

        </button>

        

        <button type="button" class="btn btn-default btn-grey btn-sm rateButton" aria-label="Left Align">

          <span class="glyphicon glyphicon-star" aria-hidden="true"></span>

        </button>

        

        <button type="button" class="btn btn-default btn-grey btn-sm rateButton" aria-label="Left Align">

          <span class="glyphicon glyphicon-star" aria-hidden="true"></span>

        </button>

        <!-- End Star -->


        <input type="hidden"  id="rating" name="rating" value="1">

        <input type="hidden"  id="itemId" name="itemId" value="<?php echo $_GET['id']; ?>">

        <input type="hidden" name="action" value="saveRating">

    </div>      

    <div class="form-group">

        <!--Comment Start-->

        <label for="usr">Title*</label>

        <input type="text"  id="title" name="title" required>

    </div>

    <div class="form-group">

        <label for="comment">Comment*</label>

        <textarea rows="5" id="comment" name="comment" required></textarea>

    </div>

    <div class="form-group">

        <button type="submit" class="btn btn-info" id="saveReview">Save Review</button>

    </div>

     <!--Comment End-->          

</form>




侃侃无极
浏览 104回答 2
2回答

POPMUISE

使用 POST 方法,您的表单在重新加载时可能会第二次提交,window.location.reload();您可以简单地redirect使用 ajax 调用成功时的页面window.location.href = "http://yourwebpage.php";或window.location.replace("http://yourwebpage.php");

倚天杖

你可以尝试下面的代码吗<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><title>Js Check</title><script src="https://code.jquery.com/jquery-3.5.1.min.js" language="javascript"></script></head><body><div id="wrapper">&nbsp; <form id="ratingForm" method="POST">&nbsp; &nbsp; <div class="form-group">&nbsp; &nbsp; &nbsp; <h4>Rate this product</h4>&nbsp; &nbsp; &nbsp; <!-- Start Star -->&nbsp; &nbsp; &nbsp; <button type="button" class="btn btn-warning btn-sm rateButton" aria-label="Left Align"> <span class="glyphicon glyphicon-star" aria-hidden="true"></span> </button>&nbsp; &nbsp; &nbsp; <button type="button" class="btn btn-default btn-grey btn-sm rateButton" aria-label="Left Align"> <span class="glyphicon glyphicon-star" aria-hidden="true"></span> </button>&nbsp; &nbsp; &nbsp; <button type="button" class="btn btn-default btn-grey btn-sm rateButton" aria-label="Left Align"> <span class="glyphicon glyphicon-star" aria-hidden="true"></span> </button>&nbsp; &nbsp; &nbsp; <button type="button" class="btn btn-default btn-grey btn-sm rateButton" aria-label="Left Align"> <span class="glyphicon glyphicon-star" aria-hidden="true"></span> </button>&nbsp; &nbsp; &nbsp; <button type="button" class="btn btn-default btn-grey btn-sm rateButton" aria-label="Left Align"> <span class="glyphicon glyphicon-star" aria-hidden="true"></span> </button>&nbsp; &nbsp; &nbsp; <!-- End Star -->&nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; <input type="hidden"&nbsp; id="rating" name="rating" value="1">&nbsp; &nbsp; &nbsp; <input type="hidden"&nbsp; id="itemId" name="itemId" value="<?php echo $_GET['id']; ?>">&nbsp; &nbsp; &nbsp; <input type="hidden" name="action" value="saveRating">&nbsp; &nbsp; </div>&nbsp; &nbsp; <div class="form-group">&nbsp;&nbsp; &nbsp; &nbsp; <!--Comment Start-->&nbsp; &nbsp; &nbsp; <label for="usr">Title*</label>&nbsp; &nbsp; &nbsp; <input type="text"&nbsp; id="title" name="title" required>&nbsp; &nbsp; </div>&nbsp; &nbsp; <div class="form-group">&nbsp; &nbsp; &nbsp; <label for="comment">Comment*</label>&nbsp; &nbsp; &nbsp; <textarea rows="5" id="comment" name="comment" required></textarea>&nbsp; &nbsp; </div>&nbsp; &nbsp; <div class="form-group">&nbsp; &nbsp; &nbsp; <button type="submit" class="btn btn-info" id="saveReview">Save Review</button>&nbsp; &nbsp; </div>&nbsp; &nbsp; <!--Comment End-->&nbsp; </form></div><script>jQuery(document).ready(function($) {&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; $('#ratingForm').on('submit', function(event){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; event.preventDefault();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; event.stopImmediatePropagation();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var formData = $(this).serialize();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $.ajax({&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; type : 'POST',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; dataType: "json",&nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; url : 'action.php',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; data : formData,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; success:function(response){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; console.log(response);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(response.success == 1) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $("#ratingForm")[0].reset();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; window.location.reload();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; });&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; });});</script></body></html>
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript