如果检查无线电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="">
你可以使用click
event:
$("#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);
});