通过ajax onchange事件传递选定的值


Pass a selected value through ajax onchange event

我有一个选择表单:

<form id="form" name="nameForm">
<select id="selectId" onchange="loadAjax('url.php');return false;">
<option>One</option
<option>Two</option>
</select>
</form>

我希望当用户更改所选选项时,它会加载带有url中值的ajax函数(通过GET)。

所以我尝试了几种方法,这是我最后一次尝试:

onchange="
var e = document.getElementById('form');
var strSel = e.form.selectId.value;
loadAjax('url.php?v='+strSel);
return false; 
">

不是很正确。有人知道如何正确地做到这一点吗?

您在options 中缺少值属性

我试过了,它在添加后有效,比如

<select id="selectId" onchange="alert('url.php?v='+this.value);return false;">
   <option value="one">One</option>
   <option value="two">Two</option>
</select>

你应该试试这个,

<select id="selectId" onchange="loadAjax('url.php?v='+this.value);return false;">
   <option value="one">One</option>
   <option value="two">Two</option>
</select>

工作Fiddle

根据您的代码示例,这将适用于

onchange="
var e = document.getElementById('selectId');
var strSel = e.options[e.selectedIndex].value;
loadAjax('url.php?v='+strSel);
return false; 
">