根据条件触发表单字段的必需属性

问题描述 投票:0回答:1

如果检查无线电1(#choice_one)如何使输入字段1(#field_one)成为必需,并且如果检查无线电2(#choice_two)如何输入表格2(#field_two)是必需的并且使用JS从输入field_one中删除所需?

<input type="radio"  id="choice_one">
 <input type="text" class="form-control" id="field_one" name="item_name" value="">
 <input type="radio" id="choice_two">
 <input type="text" class="form-control" id="field_two" name="item_name" value="">
javascript jquery
1个回答
0
投票

你可以使用clickevent:

$("#choice_one").on("click",function(){
     var checked = $(this).prop("checked");
     $("#field_one").prop("required",checked);
});
//and same thing with #choice_two

或者使用相同的功能:

$("#choice_one,#choice_two").on("click",function(){
     var checked = $(this).prop("checked");
     $(this).next().prop("required",checked);
});
© www.soinside.com 2019 - 2024. All rights reserved.