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

时间:2017-12-18 13:11:17

标签: javascript jquery

如果检查无线电一(#choice_one)如何使输入字段1(#field_one)成为必需,并且如果检查无线电二(#choice_two)如何输入两个输入(#field_two)则需要从输入中删除field_one使用JS?

<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="">

1 个答案:

答案 0 :(得分:0)

您可以使用click事件:

$("#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);
});
相关问题