2017-12-18 114 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); 
}); 
相關問題