2011-04-16 84 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> 

}

相关问题