我想根据选择选项更改我的表单操作,下面的代码from this question工作得很好:
function chgAction() {
var form = document.form;
console.log('chgAction()');
console.log(form.ddownOption.selectedIndex);
switch (form.ddownOption.selectedIndex) {
case 1:
form.action = "http://myform/option1";
break;
case 2:
form.action = "http://myform/option2";
break;
}
}
但是我还需要根据相同的选择更改隐藏字段中的值:
function chgAction() {
var form = document.form;
console.log('chgAction()');
console.log(form.ddownOption.selectedIndex);
switch (form.ddownOption.selectedIndex) {
case 1:
form.action = "http://myform/option1";
form.getElementById(option_dates).name = "option1_dates";
break;
case 2:
form.action = "http://myform/option2";
form.getElementById(option_dates).name = "option2_dates";
break;
}
}
以上不起作用,我不知道如何解决这个问题。我收到一个Function not found错误。
HTML:
<form id="form" name="form" method="post" action="" target="_blank">
<select name="ddownOption" id="ddownOption" onChange="chgAction()">
<option value="">-- Please Select --</option>
<option value='IR'>IR</option>
<option value='TN'>IR</option>
</select>
<input id="option_dates" type="hidden" name="" value="1" size="2"/>
请帮忙。
答案 0 :(得分:3)
问题在于获取隐藏元素:
1.On int totalHeight = 0;
for(int i=0; i<getItemCount(); i++){
View chView = viewHolder.itemView;
chView.measure(
View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED),
View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
totalHeight += chView.getMeasuredHeight();
totalHeight += Utils.dpToPx(8+15);
}
totalHeight += 1;
LinearLayout.LayoutParams listParams = (LinearLayout.LayoutParams) mParentContainer.getLayoutParams();
listParams.height = totalHeight;
mParentContainer.setLayoutParams(listParams);
我们无法使用form
需要使用getElementById()
。
2. ID 给出变量而不是字符串。
document
答案 1 :(得分:1)
将隐藏的更改更改为以下内容。
div.content
和
document.getElementById('option_dates').name = "option1_dates";
答案 2 :(得分:1)
您使用的是表单而不是文档。应该是&#39; document.getElementById()&#39;。您也没有将您的ID值括在引号内。
function chgAction() {
var form = document.form;
console.log('chgAction()');
console.log(form.ddownOption.selectedIndex);
switch (form.ddownOption.selectedIndex) {
case 1:
form.action = "http://myform/option1";
document.getElementById("option_dates").name = "option1_dates";
break;
case 2:
form.action = "http://myform/option2";
document.getElementById("option_dates").name = "option2_dates";
break;
}
}
答案 3 :(得分:0)
如果要更改表单操作,则应在更改选择值时更改
<script>
// firt check select event fired
$("#ddownOption").on("change",function(e){
currentVal = $(this).val();
var url = "";
var optionName = "";
switch(currentVal){
case 1:
url = "http://myform/option1";
optionName = "option1_dates";
break;
case 2:
url = "http://myform/option2";
optionName = "option2_dates";
break;
default:
url = "http://myform/defaultOption";
optionName = "defaultoption_dates";
break;
}
$("#form").attr("href",url);
$("#option_dates").attr("name",optionName);
});
</script>
在你的代码option_dates
中,它不是一个名称但没有单引号的值javascript将其视为js值
所以将代码更改为
document.getElementById('option_dates').name = "you_value";