更改事件多次触发 [英] change event fires multiple times
本文介绍了更改事件多次触发的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有通过php代码生成的HTML代码
I have this HTML code generating by php code
<tr class="edit_tr">
<td>a1</td>
<td class="edit_td">
<span id=" " class="text"></span>
<input id="144" class="25" type="text" value="">
</td>
<td class="edit_td">
<span id=" " class="text"></span>
<input id="144" class="26" type="text" value="">
</td>
<td class="edit_td">
<span id=" " class="text"></span>
<input id="144" class="27" type="text" value="">
</td>
</tr>
这是我正在使用的Javascript:
This is the Javascript I am using:
$(document).ready(function(e) {
$(".edit_tr").click(function(){
var id = $(this).attr("id");
// we have here tow states :#1 and #2
//#1 there is no Id, and thats mean we want to insert to database
if(typeof id == 'undefined'){
$(this).children("td").children("input").change(function(){
var stateId = $(this).attr("class");
var alternativeId = $(this).attr("id");
alert("state is :"+ stateId);
alert("alternativeId is :"+ alternativeId);
//return false;
});
}
//#2 there is an id, so we want to update the value
else{
alert("there is id ");
}
});
});
当我第一次更改输入值时,它可以很好地解决问题,但是当我再次尝试更改值时,change事件会触发多次
the problem when I change the value of input at first time it works fine, but when I try to change value another time, the change event fires many times
请提供任何帮助,谢谢
推荐答案
尝试此代码
$(document).ready(function(e) {
$(".edit_tr").click(function(){
var id = $(this).attr("id");
// we have here tow states :#1 and #2
//#1 there is no Id, and thats mean we want to insert to database
if(typeof id != 'undefined'){
alert("there is id ");
}
});
$(".edit_tr").find("input").change(function(){
var stateId = $(this).attr("class");
var alternativeId = $(this).attr("id");
alert("state is :"+ stateId);
alert("alternativeId is :"+ alternativeId);
//return false;
});
});
这篇关于更改事件多次触发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文