2013-10-07 57 views
0

我有其他類似的代碼。從JavaScript中的一個點擊事件到其他事件獲得價值

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

現在,添加更多的功能到該按鈕,我附上一個click事件到同一個按鈕

$("#confirm_button").live("click",function(event){ 

//Need value from the confirm box. 
//code goes here. 
}); 

如何從事件偵聽器的第一個事件的價值?我需要,如果我的代碼工作。 一種方法是我必須將此確認框移入我的click事件監聽器,但需求不允許我。 感謝

+0

嗨這是什麼意思「要求不允許我」?你可以將確認框移到jquery點擊事件。 –

+0

這些按鈕由if循環(PHP)創建,所以我無法移動該確認框。 – lokeshjain2008

+0

這不會影響到你的東西,因爲無論什麼時候滿足條件,只有當你點擊按鈕時,你的jquery點擊事件纔會觸發。 –

回答

0

你可以聲明全局變量和儲值離子這一點,所以它可以在其他功能

有點像這個

<script> 
//global variable here 
var testvariable; 

function1 
{ 
//initalize value here 
} 

function2 
{ 
//use here 
} 

</script> 
0

試試這個被用來...

$("#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 
    }  
}); 
0

Global Scope variables可能會幫助你解決你的問題。如果在function之外聲明variable,則該variable存在於global object上。

入住這link.you會得到一個非常大量的信息,有關各種scopes .. :)

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

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

0

使用jQuery像這樣的東西里面確認:

$("#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 
    }  
}); 
+0

我知道這是實現它的方法。我不想移動確認框。無論如何感謝您的意見。 – lokeshjain2008

0
<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'); 
} 
+0

我想我不能有jQuery事件處理程序。無論如何,這適合我的要求。 – lokeshjain2008