![](/img/trans.png)
[英]How to enable and disable button accordingly if i select the dropdown options in Angular js
[英]How to disable button If I select NO in dropdown list
代碼就像這樣:
<select id="productextra[@count@]">
<option value="[@option_id@]">Yes</option>
<option value="[@option_id@]">No</option>
</select>
該按鈕應禁用為:添加到購物車
編寫一個onchange
事件供您選擇並找到選定的值
<button id="btnSubmit" type="submit">Add to cart</button>
JS
$("select").on('change',function(){
if($(this).find('option:selected').text()=="No")
$("#btnSubmit").attr('disabled',true)
else
$("#btnSubmit").attr('disabled',false)
});
樣本片段
$("select").on('change',function(){ if($(this).find('option:selected').text()=="No") $("#btnSubmit").attr('disabled',true) else $("#btnSubmit").attr('disabled',false) });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <select id="productextra[@count@]"> <option value="[@option_id@]">Yes</option> <option value="[@option_id@]">No</option> </select> <button id="btnSubmit" type="submit">Add to cart</button>
var sel = document.getElementById('productextra');
var sv = sel.options[sel.selectedIndex].value;
這將給出選定選項的值,您可以在其中使用禁用按鈕
document.getElementById("myBtn").disabled = true;
您的HTML應該是這樣的:
<select id="productextra0">
<option value="0">Yes</option>
<option value="1">No</option>
</select>
<button type="submit" id="submit-button">Add to cart</button>
和javascript:
window.onload=function()
{
document.getElementById("productextra0").onchange=function()
{
if(this.options[this.selectedIndex].value==1)
{
document.getElementById("submit-button").disabled=true;
}
else
{
document.getElementById("submit-button").disabled=false;
}
}
}
我為添加到購物車按鈕添加了一個id,以便可以通過javascript document.getElementById輕松訪問它,現在為dropdown onchange事件添加了事件處理程序,這意味着每次dropdownchanges值都會觸發一個函數。 當下拉菜單更改其值時,該功能將檢查其值是否等於1,然后將禁用添加到購物車按鈕,否則將其啟用。
我還將該函數放入windows.onload中,以便我們可以確保onchange函數僅在其下拉列表就緒或已經由瀏覽器創建時才會附加到您的下拉列表中。
紐扣
<button id="btn" type="submit">Add to cart</button>
jQuery的
$("#btn").prop("disabled", true);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.