最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

php - Pass a selected value through ajax onchange event - Stack Overflow

programmeradmin3浏览0评论

I have a select form:

<form id="form" name="nameForm">

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

</form>

I want that when the user changes the selected option, it loads the ajax function with the value in the url (trough GET).

So I tried several things and this was my last try:

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

Not really correct. Does someone know how to do this correctly ?

I have a select form:

<form id="form" name="nameForm">

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

</form>

I want that when the user changes the selected option, it loads the ajax function with the value in the url (trough GET).

So I tried several things and this was my last try:

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

Not really correct. Does someone know how to do this correctly ?

Share Improve this question edited Jun 24, 2013 at 8:15 abhinav 1,2821 gold badge10 silver badges27 bronze badges asked Jun 24, 2013 at 8:04 Nicolas.Nicolas. 4531 gold badge7 silver badges27 bronze badges 2
  • 3 take a look at jquery your life will be much easier. – DevZer0 Commented Jun 24, 2013 at 8:07
  • thanks but I'm not using Jquery – Nicolas. Commented Jun 24, 2013 at 8:13
Add a ment  | 

2 Answers 2

Reset to default 2

You are missing value attribute in options

I tried it and it works after adding value, like,

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

You should try this,

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

Working Fiddle

According to your code sample this will work

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

评论列表(0)

  1. 暂无评论