最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Checkbox on change is not firing - Stack Overflow

programmeradmin4浏览0评论

I got a check box on the html page as:

      <input type="checkbox" id="chk-info" name="chk-info"  /> 

when I try to write an on change event and run, it's not getting fired. Instead if, I place an alert message before the function it is firing fine.

 alert('blah');
 var sth = function(){
 function m(){
$("input[type='checkbox']").click(function (e) {
            if ($(this).is(':checked')) {
                alert("true");
            } else {
                alert("false");
            }

        });
   }
     return{ m:m};
    };

I have tried using the id selector as well, but with not much luck. Can anyone help me in pointing where the issue is?

EDIT: When I place the check box checked function outside the above function it works well with 'onchange' event attached on the <input> tag

I got a check box on the html page as:

      <input type="checkbox" id="chk-info" name="chk-info"  /> 

when I try to write an on change event and run, it's not getting fired. Instead if, I place an alert message before the function it is firing fine.

 alert('blah');
 var sth = function(){
 function m(){
$("input[type='checkbox']").click(function (e) {
            if ($(this).is(':checked')) {
                alert("true");
            } else {
                alert("false");
            }

        });
   }
     return{ m:m};
    };

I have tried using the id selector as well, but with not much luck. Can anyone help me in pointing where the issue is?

EDIT: When I place the check box checked function outside the above function it works well with 'onchange' event attached on the <input> tag

Share Improve this question edited Jul 11, 2019 at 13:02 Brian Tompsett - 汤莱恩 5,89372 gold badges61 silver badges133 bronze badges asked Sep 3, 2017 at 1:40 John MJohn M 2011 gold badge4 silver badges17 bronze badges 5
  • Just tested this and everything is working fine, is this all the code you have ? – doubleOrt Commented Sep 3, 2017 at 1:45
  • No, I got other functions in the same page as well and I don't see any errors on the browser console. I got this event placed on the page load as well. – John M Commented Sep 3, 2017 at 1:47
  • Could you add the plete Javascript code ? What are you doing to the checkbox ? Is this the only part of the code that interacts with your checkbox ? – doubleOrt Commented Sep 3, 2017 at 1:49
  • if the checkbox is checked I need to hide a div content, that's all the JS code that is available. – John M Commented Sep 3, 2017 at 2:08
  • Well, the code you have provided works on my puter. So there are no problems with it. I am certain that something is happening in your other code. Go and create a fiddle: jsfiddle. – doubleOrt Commented Sep 3, 2017 at 2:12
Add a ment  | 

2 Answers 2

Reset to default 5

Put the onclick event of the checkbox on page load.

$(document).ready(function () {
    $("input[type='checkbox']").click(function (e) {
        if ($(this).is(':checked')) {
            alert("true");
        } else {
            alert("false");
        }
    });
})
<script src="https://ajax.googleapis./ajax/libs/jquery/3.2.1/jquery.min.js"></script>

<input type="checkbox" id="chk-info" name="chk-info" />
<label for="chk-info">label</label>

$(document).ready(function () {
    $("input[type='checkbox']").click(function (e) {
        if ($(this).is(':checked')) {
            $(this).val("true");
        } else {
            $(this).val("false");
        }
    });
})
<script src="https://ajax.googleapis./ajax/libs/jquery/3.2.1/jquery.min.js"></script>

<input type="checkbox" id="chk-info" name="chk-info" />
<label for="chk-info">label</label>

发布评论

评论列表(0)

  1. 暂无评论