亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

使用 Ajax 提交表單兩次

使用 Ajax 提交表單兩次

侃侃無極 2023-09-21 10:46:19
我嘗試使用 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>
查看完整描述

2 回答

?
POPMUISE

TA貢獻1765條經驗 獲得超5個贊

使用 POST 方法,您的表單在重新加載時可能會第二次提交,window.location.reload();您可以簡單地redirect使用 ajax 調用成功時的頁面window.location.href = "http://yourwebpage.php";window.location.replace("http://yourwebpage.php");



查看完整回答
反對 回復 2023-09-21
?
倚天杖

TA貢獻1828條經驗 獲得超3個贊

你可以嘗試下面的代碼嗎


<!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">

  <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>

</div>

<script>

jQuery(document).ready(function($) {

        

        $('#ratingForm').on('submit', function(event){

          event.preventDefault();

          event.stopImmediatePropagation();

            var formData = $(this).serialize();

            $.ajax({

                type : 'POST',

                dataType: "json",   

                url : 'action.php',                 

                data : formData,

                success:function(response){

                console.log(response);

                    if(response.success == 1) {

                        $("#ratingForm")[0].reset();

                        window.location.reload();

                    } 

                }

            }); 

        });

});

</script>

</body>

</html>



查看完整回答
反對 回復 2023-09-21
  • 2 回答
  • 0 關注
  • 137 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號