当用户在下拉列表中选择一个选项时,我试图获取表单中的所有值。我使用的是Onchange属性,但我不知道如何通过此属性将所有值发送到submitForm()。
<input type="hidden" name="var1" value="..." />
<input type="hidden" name="var2" value="..." />
<input type="hidden" name="var3" value="..." />
<select name="var4" onchange="submitForm([var1 var2 var3 var4]);return false">
<option value="1">Choice 1</option>
<option value="2">Choice 2</option>
</select>
<script>
function submitForm(var1, var2, var3, var4)
{
var OAjax;
if (window.XMLHttpRequest) OAjax = new XMLHttpRequest();
else if (window.ActiveXObject) OAjax = new ActiveXObject('Microsoft.XMLHTTP');
OAjax.open('POST',"return.php",true);
OAjax.onreadystatechange = function()
{
if (OAjax.readyState == 4 && OAjax.status==200)
{
if (document.getElementById)
{
document.getElementById('msg').innerHTML=''+OAjax.responseText+'';
}
}
}
OAjax.setRequestHeader('Content-type','application/x-www-form-urlencoded');
OAjax.send(var1='+var1+'&var2='+var2'&var3='+var3'&var4='+var4);
}
</script>
由于
答案 0 :(得分:0)
由于你的js方法中没有使用jQuery,我假设你并没有真正使用jQuery。
在那种情况下
<select name="var4" onchange="submitForm();">
然后
function submitForm() {
//get the input elements by name and then read its value
var OAjax, var1 = document.getElementsByName('var1')[0].value,
var2 = document.getElementsByName('var2')[0].value,
var3 = document.getElementsByName('var3')[0].value,
var4 = document.getElementsByName('var4')[0].value;
if (window.XMLHttpRequest) OAjax = new XMLHttpRequest();
else if (window.ActiveXObject) OAjax = new ActiveXObject('Microsoft.XMLHTTP');
OAjax.open('POST', "return.php", true);
OAjax.onreadystatechange = function () {
if (OAjax.readyState == 4 && OAjax.status == 200) {
if (document.getElementById) {
document.getElementById('msg').innerHTML = '' + OAjax.responseText + '';
}
}
}
OAjax.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
//create a parma string using the input values
OAjax.send('var1=' + var1 + '&var2=' + var2 '&var3=' + var3 '&var4=' + var4);
}