我有这个由PHP代码生成的HTML代码
<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:
$(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 ");
}
});
});
第一次更改输入值时出现问题但是当我尝试再次更改值时,更改事件会多次触发
请任何帮助,我很感激
答案 0 :(得分:4)
每次点击$(“。edit_tr”),你都会添加另一个事件处理程序,这就是代码多次触发的原因
答案 1 :(得分:3)
试试这段代码
$(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;
});
});