简体   繁体   English

更改复选框的值不会触发Jquery中的更改事件

[英]Change checkbox value doesnt fire the change event in Jquery

I have a checkbox and I marked it as checked, however it doesnt fire the on change function. 我有一个复选框,我将其标记为已选中,但是它不会触发on change功能。 The note doesn't appear. 笔记没有出现。

My code:
 $('#checkbox1').prop("checked", true);

$('#checkbox1').change(function(){
        if ($(this).is(':checked'))
            $('#s-note').show();
        else
            $('#s-note').hide();
    });

If you are setting the note to be hidden by default and doing it with an id based selector, like this: 如果您将注释设置为默认隐藏,并使用基于id的选择器进行处理,如下所示:

#s-note { display:none; }

Then your code won't be able to show it because it also uses an id based selector and that won't be more specific than the selector already in effect. 然后,您的代码将无法显示它,因为它还使用了基于id的选择器,并且不会比已经生效的选择器更具体。

Instead, you'll have to default the note to hidden using a selector that is less specific than the id selector you will use to show/hide it later. 取而代之的是,您必须使用一个选择器将注释默认为隐藏,该选择器的特定性不如您以后用来显示/隐藏它的id选择器。 That would be a class. 那将是一堂课。

Also, it's critical that you set up the event handler before you trigger the event, so that when the event happens, there is already an event handler registered. 同样,至关重要的是触发事件之前设置事件处理程序,以便在事件发生时已经注册了事件处理程序。

Now, for your needs, you don't really need the change event, click will do just fine. 现在,对于您的需求,您实际上不需要change事件, click就可以了。 And, lastly, to ensure that you properly trigger the event, use JQuery's .trigger() method to set things in motion. 最后,为确保正确触发该事件,请使用JQuery的.trigger()方法来使事件运动。

 // Make sure you set up the callback first $('#checkbox1').on("click", function(){ if ($(this).is(":checked")) $('#s-note').show(); else $('#s-note').hide(); }); // Then just trigger the event $('#checkbox1').trigger("click"); 
 .hide { display:none; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <input type="checkbox" id="checkbox1">Test <div id="s-note" class="hide">Special</div> 

Do you expect the onchange to fire without user interaction? 您是否希望onchange在无需用户干预的情况下能够启动?

Your issue is you set the checked state before you set the handler so if this would have trigged change, you would have not caught it. 您的问题是,在设置处理程序之前先设置检查状态,这样如果触发更改,就不会抓住它。 Your real issue here is setting the property with JavaScript does not fire the change event. 您真正的问题是用JavaScript设置属性不会触发更改事件。 So you need to trigger the change event manually. 因此,您需要手动触发更改事件。

$('#checkbox1')
  .prop("checked", true)  // set it to checked
  .on("change", function() {  // bind change event
    $('#s-note').toggle($(this).is(':checked')); // toggle visibility
  }).trigger("change"); //trigger that you need the change to run

I hope this code snippet will help you 我希望此代码段对您有所帮助

Approach 1
$('#idcheckbox').on('change', function() {
  if($(this).is(':checked')) {
    alert('checked');
  } else {
   alert('default ');
  }
});

Approach 2
$('input[type=checkbox]').on('change', function() {
if($(this).is(':checked')) {
    alert('checked');
  } else {
   alert('default ');
  }
});

I ran your code in .net mvc and it runs fine. 我在.net mvc中运行了您的代码,它运行正常。

This is the checkbox same id it hide or shows div element. 这是它隐藏或显示div元素的复选框相同的ID。

<input type="checkbox" id="checkbox1" value=" " />
<div id="s-note" style="display: none">
       <label>Showing</label>
</div>

What are you trying to display? 您要显示什么?

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

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