2017-08-28 262 views
0

我想啓用輸入表單時,我檢查輸入複選框。複選框啓用/禁用jquery

<div class="form-group clearfix"> 
    <input type="checkbox" id="work" class="pull-left" /> 
    <label for="work" class="form-info pull-left"> 
    {!! Form::number('work', null, ['class' => 'form-control work','placeholder' => 'Nghề nghiệp'])!!} 
    </label> 
</div> 

<div class="form-group clearfix"> 
    <input type="checkbox" id="city" class="pull-left" /> 
    <label for="city" class="form-info pull-left"> 
    {!! Form::number('city', null, ['class' => 'form-control city','placeholder' => 'Thành phố'])!!} 
    </label> 
</div> 
+0

,你的意思是不可點擊字面上禁用?禁用表單元素將阻止它在提交時處理,我會顯示/隱藏 – clearshot66

回答

0

$("input[type=checkbox]").on("click",function(){ 
 
    $("label[for="+$(this).attr("id")+"]").find("input[type=text]").prop("disabled", (!$(this).is(":checked"))); 
 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<div class="form-group clearfix"> 
 
    <input type="checkbox" id="work" class="pull-left" /> 
 
    <label for="work" class="form-info pull-left"> 
 
    <input type="text" disabled /> 
 
    </label> 
 
</div> 
 

 
<div class="form-group clearfix"> 
 
    <input type="checkbox" id="city" class="pull-left" /> 
 
    <label for="city" class="form-info pull-left"> 
 
<input type="text" disabled /> 
 
    </label> 
 
</div>

當你說啓用/禁用
0

爲了檢查複選框是否被選中,你可以做什麼這樣的事情。如果你不能讓它工作,讓我知道,我可以幫助更多。

$("#work").on("change", function(){ 
    var checked = $(this).is(':checked'); 
    if(checked){ 
     $("#idofwahteveryouwantdisabled").css("disabled", true); 
    } 
    else{ 
     $("#idofwahteveryouwantdisabled").css("disabled", false); 
    } 
}