Javascript通过Id获取元素并设置值
我有一个javascript函数,我向它传递一个参数。该参数表示我的网页中元素(隐藏字段)的id。我想更改此元素的值Javascript通过Id获取元素并设置值,javascript,default-value,Javascript,Default Value,我有一个javascript函数,我向它传递一个参数。该参数表示我的网页中元素(隐藏字段)的id。我想更改此元素的值 function myFunc(variable){ var s= document.getElementById(variable); s.value = 'New value' } 当我这样做时,我得到一个错误,即由于对象为null,无法设置该值。但是我知道这个对象不是空的,因为我在浏览器生成的html代码中看到了它。 无论如何,我尝试了以下代码来调试 functi
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'
请问我该怎么修
编辑
当页面加载时,我为其设置值的元素是hidden字段,id是动态det。我已尝试在$(document).ready函数中添加此项,但未起作用
<div id="This-is-the-real-id"></div>
我会做你想做的
给定 我会做你想做的
函数setContent(id,newvalue){
var s=document.getElementById(id);
如果(s.tagName.toUpperCase()=“输入”)s.value=newvalue;
else s s.innerHTML=newvalue;
}
addEventListener(“加载”,函数(){
setContent(“这是真实的id div”,“你好”);
setContent(“这是真实的id输入”,“你好”);
})
试试下面的方法,它会起作用的
<html>
<head>
<script>
function displayResult(element)
{
document.getElementById(element).value = 'hi';
}
</script>
</head>
<body>
<textarea id="myTextarea" cols="20">
BYE
</textarea>
<br>
<button type="button" onclick="displayResult('myTextarea')">Change</button>
</body>
</html>
函数显示结果(元素)
{
document.getElementById(element).value='hi';
}
缓冲酵母抽取物
改变
如果在textarea呈现到页面之前执行myFunc(变量),您将得到null异常错误
<html>
<head>
<title>index</title>
<script type="text/javascript">
function myFunc(variable){
var s = document.getElementById(variable);
s.value = "new value";
}
myFunc("id1");
</script>
</head>
<body>
<textarea id="id1"></textarea>
</body>
</html>
//Error message: Cannot set property 'value' of null
指数
函数myFunc(变量){
var s=document.getElementById(变量);
s、 value=“新值”;
}
myFunc(“id1”);
//错误消息:无法将属性“值”设置为null
因此,请确保页面中确实存在文本区域,然后调用myFunc,您可以使用window.onload或$(document.ready)函数。
希望对您有所帮助。
<html>
<head>
<script>
function updateTextarea(element)
{
document.getElementById(element).innerText = document.getElementById("ment").value;
}
</script>
</head>
<body>
<input type="text" value="Enter your text here." id = "ment" style = " border: 1px solid grey; margin-bottom: 4px;"
onKeyUp="updateTextarea('myDiv')" />
<br>
<textarea id="myDiv" ></textarea>
</body>
</html>
函数updateTextarea(元素)
{
document.getElementById(element).innerText=document.getElementById(“元素”).value;
}
我认为问题在于调用javascript函数的方式。您的代码如下所示:
<input type="button" onclick="javascript: myFunc(myID)" value="button"/>
myID应该用引号括起来。对于每个元素类型,您可以使用特定属性来设置值。例如:
<div id="theValue1"></div>
window.document.getElementById("theValue1").innerText = "value div";
<input id="theValue2"></input>
window.document.getElementById("theValue2").value = "value input";
window.document.getElementById(“theValue1”).innerText=“value div”;
window.document.getElementById(“theValue2”).value=“值输入”;
遇到这个问题, 没有回答提出除了使用
.value()
虽然对最初的提问者来说不太可能有帮助,
本附录实际上更改了源页面中值的内容,
这反过来又使值更新form.reset()
-proof
我希望这能帮助别人
(或者半年后我忘记了js的怪癖…让我们看看你在哪里调用这个函数(从你提供的代码来看,这个函数没有名字)。变量是什么?那么如何调用未命名函数呢?在这种情况下,当您执行诊断
alert()
或console.log()
时,您应该始终使用一些标记字符来包装值,以便可以判断字符串中是否有多余的空格字符。所以:alert(“[”+x+“],[“+y+”])代码>请在jsfiddle.net上展示一个发生这种情况的例子-你所问的没有真正意义。正是我所想的。但是我的id和设置是动态的。但我用了同样的想法。但是当调用I函数时,文档找不到具有指定ID的元素。我们需要查看html和事件处理程序(onclick或其他)内联事件处理程序,这是错误的做法。我创建隐藏字段,然后创建按钮,该按钮执行设置隐藏字段值的函数。但是,id是动态设置的。我尝试在$(document).ready函数中添加此函数,但出现了相同的问题。@jpo如果id是动态设置的,还应确保在设置id后调用myFunc。您可以尝试在设置id的函数中调用myFunc。我的代码类似于@Html.HiddenFor(m=>m.myfield,new{id=“myId”})。该函数仅在单击按钮时调用,并且仅在页面加载后才会调用。还有什么方法可以确保在单击按钮之前发生所有事情?@jpo在onclick=“javascript:myFunc(myID)”,myID应该用单引号括起来。javascript:
标签完全没有必要
<html>
<head>
<script>
function updateTextarea(element)
{
document.getElementById(element).innerText = document.getElementById("ment").value;
}
</script>
</head>
<body>
<input type="text" value="Enter your text here." id = "ment" style = " border: 1px solid grey; margin-bottom: 4px;"
onKeyUp="updateTextarea('myDiv')" />
<br>
<textarea id="myDiv" ></textarea>
</body>
</html>
<input type="button" onclick="javascript: myFunc(myID)" value="button"/>
<div id="theValue1"></div>
window.document.getElementById("theValue1").innerText = "value div";
<input id="theValue2"></input>
window.document.getElementById("theValue2").value = "value input";
document.getElementById('some-input').value="1337";
document.getElementById('some-input').setAttribute("value", "1337");