简体   繁体   English

javascript onblur函数不起作用

[英]javascript onblur function not working

I can't get onblur function at second row onwards, please help me. 从第二行开始,我无法获得onblur函数,请帮助我。 Here is my code. 这是我的代码。

javascript JavaScript的

function calc()
{
var fi = document.getElementById("one");
var se = document.getElementById("two");
var th = fi.value * 59.63;
document.getElementById("three").value=th;
} 
function addRow(){ 
//var table=document.getElementById('countTable');
var tbody=document.getElementsByTagName('TBODY')[0];
var row=document.createElement('TR');
var cell1=document.createElement('TD');
var cell2=document.createElement('TD');
var cell3=document.createElement('TD');

var cell1value='';
cell1value+='One <input type="text" name="one" id="one" value="">';
var cell2value='';
cell2value+='Two <input type="text" name="two" id="two" value="">';
var cell3value='';
cell3value+='Three <input type="text" name="three" id="three" value="">';

cell1.innerHTML=cell1value;
cell2.innerHTML=cell2value;
cell3.innerHTML=cell3value;

row.appendChild(cell1);
row.appendChild(cell2);
row.appendChild(cell3);

tbody.appendChild(row);
//alert(table+' '+row+' ');
}

HTML HTML

<table name="entry" width="100%" id="countTable">
<TBODY>
<tr>
<td>One 
<input type="text" name="one" id="one" value="" onBlur="calc();">
</td>
<td>Two
<input type="text" name="two" id="two" value="" onBlur="calc();">
</td>
<td>Three
<input type="text" name="three" id="three" value="">
</td>
</tr>
</TBODY>
</table>

<input type="button" name="add" value="Add" onClick="addRow();">

Notice the input elements in your initial row: 注意第一行中的input元素:

<input type="text" name="one" id="one" value="" onBlur="calc();">

And the input elements in your subsequent rows: 以及后续行中的输入元素:

<input type="text" name="one" id="one" value="">

There's a key difference between them which prevents the subsequent rows from making use of the onBlur event. 它们之间有一个关键的区别,那就是防止随后的行使用onBlur事件。 In order to respond to the event, just like in the first row, you need to actually attach a handler for that event. 为了响应事件,就像在第一行中一样,您实际上需要为该事件附加一个处理程序 Something like this: 像这样:

<input type="text" name="one" id="one" value="" onBlur="calc();">

Note, however, that your code has other problems. 但是请注意,您的代码还有其他问题。 Namely: 即:

  • You're re-using id values, so the behavior or anything using those id s (such as your calc function) will be undefined. 您正在重新使用id值,因此使用这些id的行为或任何内容(例如calc函数)将是不确定的。 id s need to be unique throughout the document. id在整个文档中必须是唯一的。
  • You're re-using name values. 您正在重用name值。 While allowed, this could lead to unintended side-effects when you try to use the values posted by this form. 如果允许,当您尝试使用此表单发布的值时,可能会导致意外的副作用。 Later elements may obscure the values from earlier elements. 后面的元素可能会使前面的元素中的值模糊。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM