簡體   English   中英

我想在單擊按鈕時生成bool值

[英]I want to generate a bool value when I click a button

我想只在用戶確認時執行一些腳本。
像這樣的示例代碼

if 'confirm' is clicked, then $confirm = true;   

if($confirm)
 {
   some script
 }
else
 {
   other script
 }
 ?>

有兩個按鈕'確認'和'丟棄'。 如果單擊確認,則$ confirm = true;

假設您有兩個這樣的按鈕

<input type="button" name="confirm" id="confirm" value="confirm">
<input type="button" name="discard" id="discard" value="discard">

您可以使用jquery事件處理程序來獲取像這樣單擊的按鈕

$(document).ready(function() {
   $("#confirm").on('click',function(){
     alert('Confirm clicked');
     //If you need to communicate with the server the do an ajax call here
   });

   $("#discard").on('click',function(){
     alert('Discard clicked');
     //If you need to communicate with the server the do an ajax call here
   });
});

您可以從HERE檢查點擊事件參考

您可以使用php,使用環境php變量提取字段狀態,您只需制作一個公式並將一些數據發送到您的webserver腳本文件。

<?php
$confirm = $_POST["name_of_the_button"]
if(isset($confirm))
 {
   some script
 }
else
 {
   other script
 }

- - -編輯 - - -:

我用兩個文件制作了它:

文件:form.php

<html>
<head>
<title>Button test</title>
<script type="application/javascript">
function ajaxObj()
{
   var xmlHttp=null;
   try
   {
      // Browsers Firefox, Opera 8.0+, Safari;
      xmlHttp=new XMLHttpRequest();
   }
   catch(e)
   {
      try
      {
         // Browser Internet Explorer;
         xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
      }
      catch(e)
      {
         try
         {
            // Other browsers
            xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
         }
         catch(e)
         {
            // Your browser does not suport XMLhttp object
            alert('Your browser does not suport XMLhttp object');
            return false;
         }
      }
   }
   return xmlHttp;
}

function doQuery(id)
{
    xmlHttp=ajaxObj();
    if(xmlHttp==null)
    {
        alert('Your browser does not suport XMLhttp object');
        return;
    }
    xmlHttp.open("POST","check.php",true);
    var send = 'id='+id;
    xmlHttp.onreadystatechange=function()
    {
        if (xmlHttp.readyState==4 && xmlHttp.status==200)
        {
            document.getElementById("output").innerHTML=xmlHttp.responseText;
        }
    }
    xmlHttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
    xmlHttp.send(send);
}
</script>
</head>
<body>
<button id="confirmed" name="button" onclick="doQuery(this.id)">Confirm</button>
<button id="declined" name="button" onclick="doQuery(this.id)">Decline</button>
<div id="output"></div>
</body>
</html>

文件check.php:

<?php
$action = $_POST["id"];
$confirm = false;
if($action=="confirmed")
    $confirm = true;
else if($action=="declined")
    $confirm = false;
else {}
?>

如果您使用javascript然后使用belo: -

if(window.confirm("please confirm"))
{
    return true;
    // or do Your operation.
}
else{
    return false;
    // or do Your operation.
}

否則你可以使用jquery。

<input type="button" name="confirm" id="confirm" value="confirm"> 
<input type="button" name="discard" id="discard" value="discard">

$(document).ready(function() {    
  $("input:button").on('click',function(){
    if($(this).id == 'confirm'){ 
      // put your script here
    }else{ 
      // put your script here }   
    });

});

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM