jQuery click event handler is called twice for a checkbox

前端 未结 10 1964
野趣味
野趣味 2020-12-09 17:08

I have a problem with the following code in an ASPX page:



        
10条回答
  •  生来不讨喜
    2020-12-09 17:37

    I know the question is far closed now, but I just have faced the same problem and I want to add the solution I found, may come in handy for similar problems on the future.

    When you add ASP code like:

    
    

    the problem is that is not an html control, it's just something ASP made up from the twisted mind of some psycho invented, so the browser will receive something else.

    The browser will receive something like:

    
        
        
    
    

    One of many possible solutions:

    The browser receive a span which content an input "checkbox" and a label for it with your text. Therefore my solution for this would be something like:

    $('.test > :checkbox').click(function() {
        if ($(this).attr("checked")) {
            alert("checked!!!");
        } else {
            alert("non checked!!!");
        }
    });
    

    What happened up there? This selector $('.test > :checkbox') means: find the elements with the class "test" and bring any checkbox that it contains.

提交回复
热议问题