当复选框被选中时,Jquery改变隐藏的输入值,如果未选中则反转? [英] Jquery changing hidden input value when a checkbox is checked and reverse when unchecked?

查看:225
本文介绍了当复选框被选中时,Jquery改变隐藏的输入值,如果未选中则反转?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当用户在不同表单字段中选中复选框时,我使用Jquery将隐藏输入的值更改为 True 。它看起来像这样:

  $(function(){
$('。year')。 (){
$('#carsearch')。val('True');
});

它工作得很好,除非用户改变主意,取消选中该框,隐藏输入保持 True 的值。所以如果他们改变主意,并取消选中 .year 输入,它将 #carsearch 的值更改为 False



我真的很感激任何关于如何完成的输入。


< div(= function(){
$('#carsearch'));解决方案

  .val($(this).is(':checked')?'True':'False'); 
})

你可能最好听'change'而不是'click'。


I am using Jquery to change the value of a hidden input to True when the user checks a checkbox in a different form field. It looks like this:

$(function(){
    $('.year').click(function(){
        $('#carsearch').val('True');
    });

It works great except if the user changes their mind and unchecks the box, the hidden input keeps the value of True. I want to make it so that if they change their mind and uncheck the .year input that it changes the value of #carsearch back to False.

I would really appreciate any input on how this is accomplished.

解决方案

$('.year').click(function(){
    $('#carsearch').val( $(this).is(':checked') ? 'True' : 'False' );
})

You might be better off listening to 'change' instead of 'click' though.

这篇关于当复选框被选中时,Jquery改变隐藏的输入值,如果未选中则反转?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆