admin管理员组文章数量:1426066
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 badges4 Answers
Reset to default 4You 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);
}
});
});
本文标签: javascriptUpdate form fields based on checkbox selectionsStack Overflow
版权声明:本文标题:javascript - Update form fields based on checkbox selections - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745400168a2656999.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论