javascript - 我想在单击按钮时生成 bool 值

标签 javascript php html

我想仅在用户确认时执行一些脚本。
示例代码如下

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 检查点击事件引用

关于javascript - 我想在单击按钮时生成 bool 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20701360/

相关文章:

javascript - 在 javascript 中是否可以在 jquery.Trim() 中包含其他字符

javascript - iPhone 上的移动菜单不会关闭

javascript - Angular:尝试添加验证码。错误 => 没有名称为 'captcha' 的表单控件的值访问器

php - 多页表单提交到单行 - PHP MYSQL

php - Ajax表单返回变量给php

html - HTML 中的日期时间是什么?

html - 当我使用 ngRouter 时,导航栏不会折叠

javascript - 如何正确地将 keyup() 函数应用于具有不同输入的不同 DOM 元素?

php - 地理距离搜索 MYSQL

html - GET 或 POST 是否比另一个更安全?