请检查我的以下 JavaScript 函数。我想在我的第二个函数 showUser 中调用 user。我尝试过但无法获得值(value)。它给了我未定义
。
功能一
<script>
var user;
function choose(choice){
user = choice;
alert("You got the value " + user);
}
</script>
第二个函数,我想从上面的函数调用 var user 。
<script type="text/javascript">
function showUser(str)
{
if (str=="")
{
document.getElementById("txtHint").innerHTML="";
return;
}
if (window.XMLHttpRequest)
{
xmlhttp=new XMLHttpRequest();
} else {
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.getElementById("txtHint").innerHTML=xmlhttp.responseText;
}
}
var buttonValue = document.getElementsByName('buttonpassvalue').item(0).value;
var buttonValue1 = user;
alert("checking buttonvalue1 " + buttonValue1);
var drdownValue = document.getElementsByName('shorting').item(0).value;
xmlhttp.open("GET", "productlistajax.php?q=" + drdownValue + "&version=" + buttonValue + "&version1=" + buttonValue1, true);
xmlhttp.send();
}
</script>
最佳答案
还有其他问题,这(这基本上是我所理解的你想要的)应该可以工作:
var user;
function choose(choice)
{
user = choice;
alert("You got the value " + user);
}
function showUser(str)
{
var buttonValue1 = user;
alert("checking buttonvalue1 " + buttonValue1);
}
choose('test');
showUser('whatever');
关于javascript - 如何从其他函数获取变量的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21128117/