1

我想从一个下拉菜单中调用 2 个函数。看来我只能让一个工作,但不能同时工作。寻求一些帮助来解决这个问题。这是代码,谢谢。

功能一

<script type="text/javascript">
function getCredit(str)
{
if (str=="")
  {
  document.getElementById("credit").innerHTML="";
return;
  }
if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  }
else
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
xmlhttp.onreadystatechange=function()
  {
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
    document.getElementById("credit").innerHTML=xmlhttp.responseText;
    }
  }
xmlhttp.open("GET","getcredit.php?id="+str,true);
xmlhttp.send();
}
window.onload = function() {
document.getElementsByName('company')[0].onchange();
}
</script>

这是功能2。

<script type="text/javascript">
function getTerms(str)
{
if (str=="")
  {
  document.getElementById("terms").innerHTML="";
return;
  }
if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  }
else
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
xmlhttp.onreadystatechange=function()
  {
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
    document.getElementById("terms").innerHTML=xmlhttp.responseText;
    }
  }
xmlhttp.open("GET","customertermsdropdownquery.php?id="+str,true);
xmlhttp.send();
}
window.onload = function() {
document.getElementsByName('company')[0].onchange();

}

</script>

这是调用它们的表单下拉列表。

<td><select name="company" onchange="getTerms(this.value);getCredit(this.value);">
<?php while($row = mysql_fetch_array($result)) {
  echo "<option value=\"".$row['company']."\">".$row['company']." (".$row['first']." ".$row['last'].")</option>"; } ?></select></td>

我在表单中使用 div 来显示 php。

<div id="terms"></div>
and
<div id="credit"></div>

我可以让任何一个单独工作,但不能一起工作。谢谢你的帮助。

4

1 回答 1

2

完全可以理解你想让它们分开。如何创建一个这样的新功能。

function getTermsAndCredits(value) {
   getTerms(value);
   getCredits(value);
}

然后在 onchange 事件中像这样调用它

<td><select name="company" onchange="getTermsAndCredits(this.value);">

这是一个小提琴,它应该会给你一个更好的主意。我认为没有必要像您当前在代码中那样调用 onload 函数。

于 2013-04-24T18:40:21.097 回答