Javascript没有在getElementById上读取值

时间:2014-12-03 02:53:18

标签: javascript

尝试在按下按钮时将值传播到五个输入字段。我在尝试:

<input name="common<?php echo"$timeItem"?>" id="common<?php echo"$timeItem"?>" 
class="form-control input-sm" style="width:55px; height:20px; margin:0; padding:2px;  text-align: center;" type="text" value="111">
<button type="button"

onclick='
myElement = document.getElementById("common<?php echo $temp;?>").value;

document.getElementById("time_id<?php echo $temp;?>").innerHTML = myElement;

<?php $temp=$temp-1;?>

document.getElementById("time_id<?php echo $temp;?>").innerHTML = myElement;

<?php $temp=$temp-1;?>

document.getElementById("time_id<?php echo $temp;?>").innerHTML = myElement;

<?php $temp=$temp-1;?>

document.getElementById("time_id<?php echo $temp;?>").innerHTML = myElement;

<?php $temp=$temp-1;?>

document.getElementById("time_id<?php echo $temp;?>").innerHTML = myElement'>

点击我更新。

但我被告知&#34;无法设置属性&inner;内部HTML&#39; of null&#34;。

我只是看不出为什么这没有拿起似乎拼写正确的输入文本字段的值!

编辑:这是浏览器呈现的方式 - 更易于阅读:

<td><input name="time_id176" id="time_id_id176"  ...  type="text" value="0:00">
</td>
<td><input name="time_id177" id="time_id_id177"  ...  type="text" value="0:00">
</td>
<td><input name="time_id178" id="time_id_id178"  ...  type="text" value="0:00">
</td>
<td><input name="time_id179" id="time_id_id179"  ...  type="text" value="0:00">
</td>
<td><input name="time_id180" id="time_id_id180"  ...  type="text" value="0:00">
</td>

<td> 
<input name="common180" id="common180" class="form-control ... type="text" value="111">
<button type="button"

onclick='
myElement = document.getElementById("common180").value;

document.getElementById("time_id180").innerHTML = myElement;


document.getElementById("time_id179").innerHTML = myElement;


document.getElementById("time_id178").innerHTML = myElement;


document.getElementById("time_id177").innerHTML = myElement;


document.getElementById("time_id176").innerHTML = myElement'>
Click blah blah.</button>
</td>

3 个答案:

答案 0 :(得分:0)

id="time_id_id176"

你的元素是你的,然后你正在调用

getElementById("time_id176").innerHTML

所以你需要修改你的id方案,或者你需要按名称getelem

并感谢您的编辑显示生成的html在这种情况下无法用php回答

答案 1 :(得分:0)

您的输入的ID为time_id_id176,而您的js为document.getElementById("time_id176")。应该是document.getElementById("time_id_id176")

或许您应该更改生成输入标记的代码,因为time_id_id176没有time_id176更有意义。

其次,document.getElementById("time_id176").innerHTML = myElement;不起作用。输入是一个自闭的标签,它没有innerHTML。正确的方式设置为value

document.getElementById("time_id176").value = myElement;

答案 2 :(得分:0)

您使用的是哪种浏览器?您应该使用.value代替.innerHTML将值设置为input元素。

哦,输入的ID也不匹配。