2011-04-16 86 views
0

我將通過單擊一個函數中的按鈕時使用提示來動態地從用戶接受一個值。我需要在提示中返回用戶接受的輸入,並在另一個函數中使用該輸入。在函數中返回一個動態接受的值,並在其他函數中使用它

如何在「onclick」中返回一個函數的值並將返回的值傳遞給其他函數?

請幫幫我。

在此先感謝所有人都試圖幫助我。

+0

如果你有你的HTML標記和代碼,你應該張貼,要澄清這是你在做什麼。 – 2011-04-16 15:29:34

回答

0

從您的描述來看,這聽起來像您需要配置您的函數以允許您傳遞參數。例如:

http://jsfiddle.net/guRQq/

<input id="button" type="button" value="The value from the button"/> 
<input id="text" type="text" /> 

$(document).ready(function(){ 
    $('#button').click(function(){ 
     myOtherFunction($(this).val()); 
    }); 
}); 

function myOtherFunction(passedValue) { 
    $('#text').val(passedValue); 
} 

這是使用jQuery,一個JavaScript庫。它適用於事件。

0

這可以通過多種方式完成。

實施例1 { //繞過使用參數/ PARAMS

<script> 
    function showme(answer) { 
     alert("You said your name is " + answer + "!"); 
     doSomething(answer); 
    } 

    function doSomething(name) { 
     alert("Second function called with the name \"" + name + "\".");  
    } 
</script> 

<button onclick="showme(prompt('What\'s your name?'));">Click here</button> 

值}

實施例2 { //使用全局變量

<script> 
    var name = ""; 

    function setName(answer) { 
     // Set the global variable "name" to the answer given in the prompt 
     name = answer; 

     // Call the second function, without having to pass any params 
     showName(); 
    } 

    function showName() { 
     alert("You said your name was " + name ".");  
    } 
</script> 

<button onclick="setName(prompt('What\'s your name?'));">Click here</button> 

}

例3 {//簡單方法

<script> 
    var name = ""; 

    function setName(answer) { 
     // Set the global variable "name" to the answer given in the prompt 
     name = prompt('What\'s your name?'); 

     // Call the second function, without having to pass any params 
     showName(); 
    } 

    function showName() { 
     alert("You said your name was " + name ".");  
    } 
</script> 

<button onclick="setName();">Click here</button> 

}

相關問題