1

我有这个由 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 ");

        }
    });
});

当我第一次更改输入值时的问题它工作正常,但是当我尝试再次更改值时,更改事件会触发多次

请任何帮助,我很感激

4

2 回答 2

4

每次单击 $(".edit_tr") 时,都会添加另一个事件处理程序,这就是代码多次触发的原因

于 2012-11-05T11:18:49.620 回答
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;         
   });

});
于 2012-11-05T11:29:15.367 回答