我有一个传递参数的 javascript 函数。该参数表示我的网页中一个元素(一个隐藏字段)的 id。我想改变这个元素的值。
function myFunc(variable){
var s= document.getElementById(variable);
s.value = 'New value'
}
当我这样做时,我得到一个错误,该值无法设置,因为对象为空。但是我知道这个对象不是 null,因为我在浏览器生成的 html 代码中看到了它。 不管怎样,我尝试了下面的代码来调试
function myFunc(variable){
var x = variable;
var y = 'This-is-the-real-id'
alert(x + ', ' + y)
var s= document.getElementById(x);
s.value = 'New value'
}
当出现警报消息时,两个参数都是相同的,但我仍然得到错误。但只要我这么做,一切都会好起来的
var s= document.getElementById('This-is-the-real-id');
s.value = 'New value'
请问我该怎么办
剪辑
我为其设置值的元素是隐藏字段,id 在页面加载时被动态设置。我已经尝试在 $(文档)中添加这个。准备功能,但没有工作