简体   繁体   English

更改事件触发多次

[英]change event fires multiple times

I have this HTML code generating by php code 我有这个通过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>

This is the Javascript I am using: 这是我正在使用的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 ");

        }
    });
});

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 第一次更改输入值时的问题可以正常工作,但是当我再次尝试更改值时,change事件会触发多次

please any help, I appreciate it 请任何帮助,我很感激

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

try this code 试试这个代码

$(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;         
   });

});

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

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