标签点击触发(“更改”)不起作用

时间:2019-05-15 12:30:15

标签: javascript jquery

每当单击相应的标签时,我想在输入上调用change事件。这是我在jQuery中尝试过的方法:

$(document).on("mouseup touchend", "label", function() {
  $("#" + $(this).attr("for")).trigger("change");
});

$(".my_example_input").on("change", function() {
  alert("Change called!");
});

1 个答案:

答案 0 :(得分:0)

尝试一下,希望对您有所帮助。我想这就是您的期望,谢谢

$('label,#checkbox_id').click(function(e){
    $('.my_example_input').trigger('change')
 
})

 $('.my_example_input').change(function(e){
     console.log('input has been changed')
   
 })
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input type="checkbox" name="checkbox" id="checkbox_id" value="value">
<label for="checkbox_id">Text</label>

<input class='my_example_input'>

相关问题