Internet Explorer 6 和 8 onchange 不工作
我正在尝试使用选择框的 onchange 方法填充表单上的某些字段。
<tr>
<td align="left"><label>*Select Branch:</label></td>
<td><form:select tabindex="5" path="userInputBranch" onchange="setProcessorBranchLocationOnChange()">
<form:option value="--" label="--" />
<form:options items="${ customerAddEntity.selectionContentsBranch}" itemLabel="key" itemValue="value" />
</form:select>
<form:hidden path="autoUserInputProcessor" />
<form:hidden path="autoUserInputCompany" />
<form:hidden path="autoUserInputLocation" />
</td>
</tr>
Javascript 函数:
function setProcessorBranchLocationOnChange() {
var rawBranch = $('#userInputBranch').attr('value').split('-');
$('#autoUserInputProcessor').attr('value', rawBranch[0]);
$('#autoUserInputCompany').attr('value', rawBranch[1]);
$('#autoUserInputLocation').attr('value', rawBranch[2]);
if(rawBranch[3]!=null){
var cBranch = rawBranch[3].split('=');
$('#userInputBranch').attr('value', cBranch[1]);
}
}
如您所见,我正在尝试使用所选下拉值的子字符串值填充隐藏字段 autoUserInputProcessor、Company、Location。
这在 Chrome 和 IE7 中运行良好。我没有收到有关 IE6 和 IE8 中隐藏字段的任何更新。我正在使用 spring 的标签库来生成表单。任何帮助将不胜感激。如果您需要任何其他信息,请告诉我。
我正在使用 jQuery 1.61.min.js 以及 jQuery forms 2.4 和 jQuery UI 1.8。
以下是 Spring 生成的 select 的一些示例数据。
这是选项之一。 (我无法控制该值的格式,它是我收到的格式。)
<option value="06-04-22-id=2504">Oneonta, NY</option>
这是填充字段的示例。
<input id="autoUserInputProcessor" name="autoUserInputProcessor" type="hidden" value="06">
<input id="autoUserInputCompany" name="autoUserInputCompany" type="hidden" value="04">
<input id="autoUserInputLocation" name="autoUserInputLocation" type="hidden" value="22">
这是我在页面加载时加载的完整 JavaScript 文件:
//window.onbeforeunload = confirmExit;
function setProcessorBranchLocationOnChange() {
var rawBranch = $('#userInputBranch').attr('value').split('-');
$('#autoUserInputProcessor').attr('value', rawBranch[0]);
$('#autoUserInputCompany').attr('value', rawBranch[1]);
$('#autoUserInputLocation').attr('value', rawBranch[2]);
if(rawBranch[3]!=null){
var cBranch = rawBranch[3].split('=');
$('#userInputBranch').attr('value', cBranch[1]);
}
}
$(function() {
// Set tab indicies that aren't available until load.
$('#customersearchanchor').attr('tabIndex', 120);
$('#logoutLink').attr('tabIndex', 125);
// Set the processor, company, location
// hidden fields on branch select change
setProcessorBranchLocationOnChange();
});
I am trying to populate some fields on a form using the onchange method of a select box.
<tr>
<td align="left"><label>*Select Branch:</label></td>
<td><form:select tabindex="5" path="userInputBranch" onchange="setProcessorBranchLocationOnChange()">
<form:option value="--" label="--" />
<form:options items="${ customerAddEntity.selectionContentsBranch}" itemLabel="key" itemValue="value" />
</form:select>
<form:hidden path="autoUserInputProcessor" />
<form:hidden path="autoUserInputCompany" />
<form:hidden path="autoUserInputLocation" />
</td>
</tr>
The Javascript function:
function setProcessorBranchLocationOnChange() {
var rawBranch = $('#userInputBranch').attr('value').split('-');
$('#autoUserInputProcessor').attr('value', rawBranch[0]);
$('#autoUserInputCompany').attr('value', rawBranch[1]);
$('#autoUserInputLocation').attr('value', rawBranch[2]);
if(rawBranch[3]!=null){
var cBranch = rawBranch[3].split('=');
$('#userInputBranch').attr('value', cBranch[1]);
}
}
As you can see I am trying to populate the hidden fields, autoUserInputProcessor, Company, Location with a subString value of the selected drop down value.
This works fine in Chrome and IE7. I am not getting any updates on the hidden fields in IE6 and IE8. I am using spring's tag lib to generate the form. Any help would be greatly appreciated. Please let me know if you need any additional information.
I am using jQuery 1.61.min.js with jQuery forms 2.4 and jQuery UI 1.8.
Here is some sample data from the select that is generated by Spring.
This is one of the options. ( I have no control over the format of the value, it is as I received it.)
<option value="06-04-22-id=2504">Oneonta, NY</option>
This is an example of the fields populated.
<input id="autoUserInputProcessor" name="autoUserInputProcessor" type="hidden" value="06">
<input id="autoUserInputCompany" name="autoUserInputCompany" type="hidden" value="04">
<input id="autoUserInputLocation" name="autoUserInputLocation" type="hidden" value="22">
This is my complete javascript file that is loaded when the page loads:
//window.onbeforeunload = confirmExit;
function setProcessorBranchLocationOnChange() {
var rawBranch = $('#userInputBranch').attr('value').split('-');
$('#autoUserInputProcessor').attr('value', rawBranch[0]);
$('#autoUserInputCompany').attr('value', rawBranch[1]);
$('#autoUserInputLocation').attr('value', rawBranch[2]);
if(rawBranch[3]!=null){
var cBranch = rawBranch[3].split('=');
$('#userInputBranch').attr('value', cBranch[1]);
}
}
$(function() {
// Set tab indicies that aren't available until load.
$('#customersearchanchor').attr('tabIndex', 120);
$('#logoutLink').attr('tabIndex', 125);
// Set the processor, company, location
// hidden fields on branch select change
setProcessorBranchLocationOnChange();
});
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尝试 jQuery 的更改功能?它可能会为您处理一些浏览器的怪异问题。
不要忘记删除 HTML 中 select 的 onchange 属性。
Try jQuery's change function? It will likely handle some browser quirkiness for you.
Don't forget to remove the onchange attribute of the select in the HTML.
首先,userInputBranch不是元素的id
其次,使用 jQuery 的
.val()
函数,而不是 attr)'value');由于您已经在使用 jquery,因此请使用
.change()
事件。First, userInputBranch is not the id of the element
Second, use the jQuery's
.val()
function, instead of attr)'value');And since you are already using jquery, use the
.change()
event.