Javascript 如何根据选项值更改HTML选择名称
如何获得期权价值。我想根据所选选项值更改Javascript 如何根据选项值更改HTML选择名称,javascript,Javascript,如何获得期权价值。我想根据所选选项值更改name=”“。 这样我就可以将name值与onChange=“”一起发送到spring控制器 我想根据选项选择的值更改选择name=“report/leavereport/weeklysummery” <select name="" onChange="document.getReportAll.submit()"> <option value="">Select Report Type</option
name=”“
。
这样我就可以将name值与onChange=“”
一起发送到spring控制器
我想根据选项选择的值更改选择name=“report/leavereport/weeklysummery”
<select name="" onChange="document.getReportAll.submit()">
<option value="">Select Report Type</option>
<option value="report">TIME REPORT</option>
<option value="leavereport">LEAVE REPORT</option>
<option value="weeklysummery">TIME SUMMARY</option>
</select>
选择报告类型
时间报告
请假报告
时间摘要
谢谢,考虑到代码和现代浏览器,最短的更改是:
onChange="this.name = this.value; document.getReportAll.submit()"
…因为在属性事件处理程序中,
此
引用select元素,现代浏览器在单选中将选项值反映为值
。将id设置为选择
<select id="select"...>...</select>
下面是一个可能的(不引人注目的)解决方案,使用getAttribute
和setAttribute
脚本侦听任何选项的单击,然后根据选项的值属性值更新选择的名称属性值
var options=document.getElementsByTagName('option');
函数changeName(){
var value=this.getAttribute('value');
window.alert('name=“”+value+”);
this.parentNode.setAttribute('name',value);
}
对于(变量i=0;i
选择报告类型
时间报告
请假报告
时间摘要
document.getElementsByTagName(“选择”)[0]。name=document.getElementsByTagName(“选择”)[0]。值这似乎是一个非常奇怪的要求。通常,表单提交之间的字段名是一致的,字段值也会有所不同。如果有人在未启用JavaScript的情况下访问,您会遇到问题吗?
var selector = document.getElementById('select');
selector.onchange = function() {
var value = this.value;
if(!value) {
for(var i = 0, op; op = selector.children[i]; i ++) {
// Check if the attribute selected is valid
if(op.getAttribute('selected')) {
value = op.value;
break;
}
}
}
this.name = value;
};