我正在尝试通过使用 javascript(没有 JQuery,只是普通的 javascript)连接其他文本字段来填充隐藏字段。
我下面的代码在 Chrome 和 Firefox 中完美运行,但在 IE 8、9、10 中失效
Javascript
function buildhidden() {
var joinedvalues = textfield_id_1.value+textfield_id_2.value+textfield_id_3.value;
document.getElementById("hiddenfield_id").value = joinedvalues;
};
html 输入文件
<input type="text" name="textfield_id_1" id="textfield_id_1" value="" maxlength="1" onKeyUp="buildhidden();">
隐藏域
<input type="hidden" name="hiddenfield_id" id="hiddenfield_id" value="" />
在 IE 中,我得到“textfield_id_1 is not defined”,仅此而已。
如有任何帮助,我们将不胜感激。我已经尝试明确声明每个文本字段:
var text1 = document.getElementById("hiddenfield_id").value;
var joinedvalues = text1+text2+ etc.
这也行不通。我是一名 php 开发人员,而 JS 不是我的强项...非常欢迎任何帮助。
最佳答案
使用 document.getElementById('id here ')
因为所有浏览器都不会将元素的 ID 公开到全局范围。
id.value // bad practice, not cross-browser
document.getElementById('id').value // good, cross browser
那么这样做:
function buildhidden() {
var joinedvalues = document.getElementById('textfield_id_1').value + document.getElementById('textfield_id_2').value + document.getElementById('textfield_id_3').value;
document.getElementById("hiddenfield_id").value = joinedvalues;
}
关于javascript - 从其他文本字段javascript填充隐藏字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22836841/