在 JavaScript 中从一次点击事件到其他事件获取价值

Get value from one click event to other event in JavaScript

本文关键字:事件 其他 获取 一次 JavaScript      更新时间:2023-09-26

我有其他人写的代码。

<button onclick="javascript:confirm("Are you sure?");" id="confirm_button"/>

现在,为了向此按钮添加更多功能,我将一键式事件附加到同一按钮

$("#confirm_button").live("click",function(event){
//Need value from the confirm box.
//code goes here.
});

如何从事件侦听器中的第一个事件中获取值?我需要我的代码是否可以工作。一种方法是我必须将此确认框移动到我的单击事件侦听器中,但要求不允许我。谢谢

你可以声明一个全局变量和存储值离子,所以它可以在其他函数中使用

有点像这样

<script>
//global variable here
var testvariable;
function1
{
//initalize value here
}
function2
{
//use here
}
</script>

Global Scope variables可能会帮助您解决问题。如果一个variable是在function之外声明的,那么这个variable存在于global object上。

查看此链接。您将获得有关各种scopes的大量信息.. :)

<script>
      var globalVariable=  "someValue";//Use this variable anywhere u may need..
</script>

http://learn.jquery.com/javascript-101/scope/

在 jquery 中使用 confirm 像这样:

$("#confirm_button").live("click",function(event){
    var valid = confirm("Are you sure?");
    if(valid){
        //do something
        var myVal = $("YOUR SELECTOR").val(); // to get the value of your selector.
    }else{
        // do something else
    }    
});

试试这个...

$("#confirm_button").live("click",function(event){
    var isvalid = confirm("Are you sure?");
    if(isvalid){
    // to get the value of your selector.
    }
    else
    {
    // do something else
   }    
});
<input type="button" onclick="var tmp=confirm('Are you sure?');if(tmp) foo(); "  
 id="confirm_button" value="btn"/>
//and in javascript function write ur code
function foo()
{
alert('add functions');
}