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

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

如果條件為真,檢查驗證腳本不返回

如果條件為真,檢查驗證腳本不返回

PHP
慕妹3146593 2023-09-08 14:20:45
我有一個包含復選框的表單:<form action="tienda3.php">    <div class="form-group">        <label for="email">Email address:</label>        <input type="email" class="form-control" id="email" name="email" placeholder="Enter your email to confirm the order">    </div>    <div class="form-group">        <div class="checkbox">            <label><input type="checkbox" id="TOS" value="This"> I certify that I am of legal age and I have read and agree to the                 <a href="../terms.php" target="_blank">Terms of use</a> and                <a href="../privacy.php" target="_blank"> Privacy Policy </a>of Sdocks LLC</label>            </div>        </div>        <button type="submit" onclick="validate()" class="btn btn-primary">Submit</button>    </form>我需要驗證用戶是否選中復選框以將表單發布到 tienda3.php。我使用此腳本來驗證用戶是否已選中該復選框:<script type=text/javascript>    function validate(){        if (document.getElementById('TOS').checked){            alert("checked") ;        }else{            alert("You didn't check it! Let me check it for you.");            return true;        }    }</script>如果選中該復選框,則表單將發布到 tienda3.php,否則必須顯示警報,通知用戶必須選中該復選框才能繼續該過程。就我而言,表單始終發布到 tienda3.php。該腳本檢測復選框是否被選中,但在這兩種情況下,表單始終打開文件 tienda3.php
查看完整描述

2 回答

?
慕村9548890

TA貢獻1884條經驗 獲得超4個贊

我建議您進行以下更改:


/* replace this:

?* <form action="tienda3.php"> */


?<form action="tienda3.php" onsubmit="return validate(event)" >


?/* replace this:

? * <button type="submit" onclick="validate()" class="btn btn-primary">Submit</button> */

<button type="submit" class="btn btn-primary">Submit</button>


/* and replace the validate() function with: */

function validate(event){

? if (document.getElementById('TOS').checked){

? ? alert("checked") ;

? ? return true;

? } else {

? ? event.preventDefault();

? ? alert("You didn't check it! Let me check it for you.");

? ? return false;

? }

}

讓我知道它是否按預期工作。

查看完整回答
反對 回復 2023-09-08
?
絕地無雙

TA貢獻1946條經驗 獲得超4個贊

我更喜歡使用ajax請求而不是表單操作


超文本標記語言


<form>

    <div class="form-group">

       <label for="email">Email address:</label>

       <input type="email" class="form-control" id="email" name="email" placeholder="Enter your email to confirm the order">

   </div>


   <div class="form-group">

        <div class="checkbox">

            <label><input type="checkbox" id="TOS" value="This"> I certify that I am of legal age and I have read and agree to the 

            <a href="../terms.php" target="_blank">Terms of use</a> and

            <a href="../privacy.php" target="_blank"> Privacy Policy </a>of Sdocks LLC</label>

        </div>

    </div>

 <button type="button" onclick="SubmitRequest()" class="btn btn-primary">Submit</button>

</form>

js


function SubmitRequest() {

  if (document.getElementById('TOS').checked){

  var postObj = {

      email: $('#email').val(),

  };

  $.ajax({

      url: "/tienda3.php",

      data: JSON.stringify(postObj),

      type: "POST",

      contentType: "application/json;charset=utf-8",

      dataType: "json",

      success: function (result) {

       console.log(result)

      },

      error: function (errormessage) {

          console.log(errormessage);

      }

  });

}

});


查看完整回答
反對 回復 2023-09-08
  • 2 回答
  • 0 關注
  • 117 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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