gpt4 book ai didi

javascript - 如何从其他函数获取变量的值

转载 作者:行者123 更新时间:2023-11-28 01:39:14 24 4
gpt4 key购买 nike

请检查我的以下 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com