javascript - Update form fields based on checkbox selections - Stack Overflow

I have a form with 6 check-boxes and 6 hidden text fields which need to be changed based on checkbox se

I have a form with 6 check-boxes and 6 hidden text fields which need to be changed based on checkbox selections

HTML:

<input type="checkbox" value="Auto" id="auto_Auto" name="auto" class="checkbox form_elem_auto"><label for="auto_Auto" id="label_auto">Auto</label>
<br />

<input type="checkbox" value="Home" id="home_Home" name="home" class="checkbox form_elem_home"><label for="home_Home" id="label_home">Home</label>
<br />

<input type="checkbox" value="Life" id="life_Life" name="life" class="checkbox form_elem_life"><label for="life_Life" id="label_life">Life</label>
<br />

<input type="checkbox" value="Health" id="health_Health" name="health" class="checkbox form_elem_health"><label for="health_Health" id="label_health">Health</label>
<br />

<input type="checkbox" value="Medicare" id="medicare_Medicare" name="medicare" class="checkbox form_elem_medicare"><label for="medicare_Medicare" id="label_medicare">Medicare</label>
<br />

<input type="checkbox" value="Renters" id="renters_Renters" name="renters" class="checkbox form_elem_renters"><label for="renters_Renters" id="label_renters">Renters</label>
<br />


<input id="auto_h" name="auto_h" class="hidden" value="FALSE" ><br />
<input id="home_h" name="home_h" class="hidden" value="FALSE" ><br />
<input id="life_h" name="life_h" class="hidden" value="FALSE" ><br />
<input id="health_h" name="health_h" class="hidden" value="FALSE" ><br />
<input id="medicare_h" name="medicare_h" class="hidden" value="FALSE" ><br />
<input id="renters_h" name="renters_h" class="hidden" value="FALSE" >

I would like to when checkbox id="auto_Auto" name="auto" is checked, the hidden field be updated with TRUE and when left unchecked to be updated/remain at FALSE. Similarly with other check-boxes and their hidden form equivalents

I have tried modifying the jQuery triggers from this SO answer but whenever check-boxes are checked, all the hidden fields get updated with TRUE

Here's the demo

I have a form with 6 check-boxes and 6 hidden text fields which need to be changed based on checkbox selections

HTML:

<input type="checkbox" value="Auto" id="auto_Auto" name="auto" class="checkbox form_elem_auto"><label for="auto_Auto" id="label_auto">Auto</label>
<br />

<input type="checkbox" value="Home" id="home_Home" name="home" class="checkbox form_elem_home"><label for="home_Home" id="label_home">Home</label>
<br />

<input type="checkbox" value="Life" id="life_Life" name="life" class="checkbox form_elem_life"><label for="life_Life" id="label_life">Life</label>
<br />

<input type="checkbox" value="Health" id="health_Health" name="health" class="checkbox form_elem_health"><label for="health_Health" id="label_health">Health</label>
<br />

<input type="checkbox" value="Medicare" id="medicare_Medicare" name="medicare" class="checkbox form_elem_medicare"><label for="medicare_Medicare" id="label_medicare">Medicare</label>
<br />

<input type="checkbox" value="Renters" id="renters_Renters" name="renters" class="checkbox form_elem_renters"><label for="renters_Renters" id="label_renters">Renters</label>
<br />


<input id="auto_h" name="auto_h" class="hidden" value="FALSE" ><br />
<input id="home_h" name="home_h" class="hidden" value="FALSE" ><br />
<input id="life_h" name="life_h" class="hidden" value="FALSE" ><br />
<input id="health_h" name="health_h" class="hidden" value="FALSE" ><br />
<input id="medicare_h" name="medicare_h" class="hidden" value="FALSE" ><br />
<input id="renters_h" name="renters_h" class="hidden" value="FALSE" >

I would like to when checkbox id="auto_Auto" name="auto" is checked, the hidden field be updated with TRUE and when left unchecked to be updated/remain at FALSE. Similarly with other check-boxes and their hidden form equivalents

I have tried modifying the jQuery triggers from this SO answer but whenever check-boxes are checked, all the hidden fields get updated with TRUE

Here's the demo

Share Improve this question edited May 23, 2017 at 11:50 CommunityBot 11 silver badge asked Feb 12, 2016 at 13:07 valdronivaldroni 1681 gold badge3 silver badges19 bronze badges
Add a ment  | 

4 Answers 4

Reset to default 4

You can target specific input with this code:

$("input[type=checkbox]").on('change', function () {
    var name = $(this).attr('name') + '_h';
    $('#' + name).val($(this).is(':checked') ? 'TRUE' : 'FALSE');
}).change();
$("input[type=text]").on('keyup', function () {
    var name = $(this).attr('name').replace(/_h$/, '');
    if ($(this).val()) {
        $('[name="' + name + '"]').prop('checked', true);
    } else {
        $('[name="' + name + '"]').prop('checked', false);
    }
});

FIDDLE

$(function () {
    $("input[type=checkbox]").on('change', function () {
       var name = $(this).attr('name');
       $("#"+name+"_h").val($(this).is(':checked') ? 'TRUE' : 'FALSE'); 

    });
    var name = $(this).attr('name').replace(/_h$/, '');
    if ($(this).val()) {
      $('[name="' + name + '"]').prop('checked', true);
   } else {
      $('[name="' + name + '"]').prop('checked', false);
   }
});

You always have reference using name. Your hidden input has id: CLICKED_OBJECT.name+"_h" UPDATE:

Set value to "false" by default

<input type="text" id="auto_h" name="auto_h" class="hidden" value='FALSE'><br />
<input type="text" id="home_h" name="home_h" class="hidden"  value='FALSE'><br />
<input type="text" id="life_h" name="life_h" class="hidden"  value='FALSE'><br />
<input type="text" id="health_h" name="health_h" class="hidden"  value='FALSE'><br />
<input type="text" id="medicare_h" name="medicare_h" class="hidden"  value='FALSE'><br />
<input type="text" id="renters_h" name="renters_h" class="hidden"  value='FALSE'>

Try searching for the hidden field based on it's attribute

$("[name="+$(this).attr('name')+"_h]").val($(this).is(':checked') ? 'TRUE' : 'FALSE');

Here's the fiddle

Looks like $.nextAll is setting values for all the fields.

$(function () {
    $("input[type=checkbox]").on('change', function () {

        //$(this).nextAll('.hidden').val($(this).is(':checked') ? 'TRUE' : 'FALSE');
        $('#'+$(this).attr('name')+'_h').val($(this).is(':checked') ? 'TRUE' : 'FALSE');
    });
    $("input[type=text]").on('keyup', function () {
        if ($(this).val()) {
            $(this).prevAll('.auto_h:first').prop('checked', true);
        } else {
            $(this).prevAll('.auto_h:first').prop('checked', false);
        }
    });
});

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745400168a4626063.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信