2017-12-18 117 views
0

如果选中无线电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=""> 

回答

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); 
}); 
相关问题