2017-07-31 46 views
1

我想在加載頁面時禁用下拉列表並使用JQuery在按鈕單擊事件上啓用它。我試過下面的代碼,但它不工作。發生的情況是禁用頁面加載列表,然後啓用按鈕單擊事件列表一秒鐘,然後再次禁用列表。想要禁用頁面加載時的下拉列表,並在HTML中的按鈕單擊事件中啓用它

$(document).ready(function() { 
debugger; 
if (jQuery('#btnViewHistoricData').data('clicked')) { 
$("#ddlBranch").prop("disabled", false); 
} else { 
$("#ddlBranch").prop("disabled", true); 
} 
}); 

<div class="text-center"> 
<button id="btnViewHistoricData" class="btn bg-dark">View Historic Data</button> 
</div> 

<div class="col-md-3"> 
<div class="form-group m-r-sm"> 
<label for='rf_name'>Branch</label> 
<select id="ddlBranch" class="m-b-sm w-lg form-control" onchange="ViewHistoricData()"> 
<option value="ALL" selected="selected">ALL</option> 
</select> 
</div> 

+0

如果你擺脫了其他塊,這有幫助嗎? – user3080953

+0

我想禁用頁面laod上的列表並啓用它按鈕單擊,這就是爲什麼我選擇瞭如果條件 – Prakash

+0

每當'btnViewHistoricData'沒有按下時,列表被禁用 – user3080953

回答

0

當您單擊該按鈕,你會做一個表單提交,導致重新裝入文檔的下拉列表設置爲禁用。

所以一定類型的按鈕=按鈕

<button id="btnViewHistoricData" class="btn bg-dark" type="button">View Historic Data</button> 

現在,當按鈕被點擊也不會有表單提交,以便將DropDownList可以啓用。

<script type="text/javascript"> 
    $(document).ready(function() { 
     $("#ddlBranch").prop("disabled", "disabled"); 

     $("#btnViewHistoricData").click(function() { 
      $("#ddlBranch").removeAttr("disabled"); 
     }); 
    }); 
</script> 
1

試試這個代碼。

$(document).ready(function() { 
 

 
$("#btnViewHistoricData").click(function(){ 
 
    $("#ddlBranch").removeAttr("disabled"); 
 

 
}); 
 

 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<div class="text-center"> 
 
<button id="btnViewHistoricData" class="btn bg-dark">View Historic Data</button> 
 
</div> 
 

 
<div class="col-md-3"> 
 
<div class="form-group m-r-sm"> 
 
<label for='rf_name'>Branch</label> 
 
<select id="ddlBranch" class="m-b-sm w-lg form-control" onchange="ViewHistoricData()" disabled="disabled"> 
 
<option value="ALL" selected="selected">ALL</option> 
 
</select> 
 
</div>

+0

在結束它再次禁用列表 – Prakash

+0

我是沒有得到你可以請你解釋更多? –

+0

啓用下拉菜單後,再次禁用下拉列表 – Prakash

0
$(document).ready(function() { 
debugger; 
if (jQuery('#btnViewHistoricData').on('clicked')) { // Use `on` instead 
$("#ddlBranch").prop("disabled", false); 
} else { 
$("#ddlBranch").removeAttr("disabled"); 
} 
}); 
+0

按預期工作不正常。它不禁用頁面加載列表 – Prakash

相關問題