这是我的javascript代码,我想将选项的当前选定值传递给我的js函数,在此代码中我使用的是静态编号6.
<select name='project_name' class='required input_field' id='project_name' onchange="sendRequest('GET','getClientName.jsp?ProjectId=6')">
<option value=''>-- Select --</option>
<option value="1">Project 1</option>
<option value="2">Project 2</option>
<option value="3">Project 3</option>
</select>
帮我解决这个问题......
答案 0 :(得分:7)
将字符串'getClientName.jsp?ProjectId=6'
更改为
'getClientName.jsp?ProjectId=' + this.options[this.selectedIndex].value)
或
'getClientName.jsp?ProjectId=' + this.value)
但我认为第一个更兼容浏览器。
答案 1 :(得分:2)
var selectBox = document.getElementById('project_name');
selectBox.addEventListener('change', function() {
sendRequest('GET', 'getClientName.jsp?ProjectId=' + this.value);
});
答案 2 :(得分:0)
它将帮助您获取所选的选项值。试试这个链接http://jsfiddle.net/xyaaa/9/。
<html>
<head>
<script>
function getOption(t){
var val;
var options = t.options;
for(var i=0,len=options.length;i<len;i++){
var option = options[i];
if(option.selected){
val = option.value;
}
}
return val;
}
function sendRequest(method , url){
console.log(url);
}
</script>
</head>
<body>
<select onchange="var x=getOption(this);sendRequest('GET','getClientName.jsp?ProjectId='+x)">
<option value="1">Project 1</option>
<option value="2">Project 2</option>
<option value="3">Project 3</option>
</select>
</body>
</html>