2012-03-31 34 views
0

好吧,我有这段代码从我从W3schools: -如何使用xmlhttp.open发送多个变量?

<html> 
<head> 
<script type="text/javascript"> 
function showCustomer(str) 
{ 
var xmlhttp;  
if (str=="") 
    { 
    document.getElementById("txtHint").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("txtHint").innerHTML=xmlhttp.responseText; 
    } 
    } 
xmlhttp.open("GET","getcustomer.asp?q="+str,true); 
xmlhttp.send(); 
} 
</script> 
</head> 
<body> 

<form action=""> 
<select name="customers" onchange="showCustomer(this.value)"> 
<option value="">Select a customer:</option> 
<option value="ALFKI">Alfreds Futterkiste</option> 
<option value="NORTS ">North/South</option> 
<option value="WOLZA">Wolski Zajazd</option> 
</select> 
</form> 
<br /> 
<div id="txtHint">Customer info will be listed here...</div> 

</body> 
</html> 

现在我做了一个表格,我通过两个变量,所以我怎么会通过这个xmlhttp.open("GET","getcustomer.asp?q="+str,true);两个变量的值。因为这件事没有被包括在内。

+0

你为什么不从事JQuery的,它简单,因为它可以... – rkosegi 2012-03-31 14:47:39

+1

jQuery也可以做到这一点?你有任何我可以参考的源链接,并可以开始学习。 – 2012-03-31 14:49:36

+1

这是怎么回事:http://stackoverflow.com/questions/886878/can-i-get-more-variables-instead-of-string-in-jquery-ajax – rkosegi 2012-03-31 14:54:09

回答

2

这很容易。对于第一个变量,使用?,对于后面的变量,使用&。例如。

var variables = "name=David&string=Hello World"; 
xmlhttp.open("GET","getcustomer.asp?" + variables,true); 

而获得的变量形成等形式,将其设置为具有ID的(最简单的方法),并做

document.getElementById('theid').value; 
+0

如果我想发送两个值从onchange = “showCustomer(this.value)',同样让函数知道它需要两个变量。 – 2012-03-31 14:58:55

+0

我无法理解这段代码'document.getElementById('theid')。value;'你能帮我吗请理解 – 2012-03-31 15:04:27