![](/img/trans.png)
[英]get Value of Disabled (Disabled & Selected previously) option in select box
[英]Toggle disabled attribute if select box option has value
我有一個默認情況下處於禁用狀態的提交按鈕,並且src屬性中的圖像為灰色。
這是HTML:
<form>
<select id="item_location">
<option value="">- Choose Destination -</option>
<option value="US">US</option>
<option value="CN">Canada</option>
<option value="IN">International</option>
</select>
<input class="submit" type="image" src="http://website.com/images/btn_buynow_LG--disabled.png" border="0" name="submit" disabled>
</form>
默認情況下,用戶必須選擇國家。 選擇具有值的國家/地區后,只要下拉選項的值不為空,我想更新圖像並刪除禁用的屬性。
到目前為止,這是我想到的jQuery,但它需要根據select item_location
選擇框的值來切換Disabled屬性。
function submitButton() {
jQuery(".submit").change(function() {
if (jQuery("#item_location") !== "" {
jQuery(".submit").attr("src", "http://www.paypalobjects.com/en_US/i/btn/btn_buynow_LG.gif").removeAttr("disabled");
});
});
}
你可以這樣做:
jQuery(".submit").prop("disabled", jQuery("#item_location").val() === "")
如果item_location
值為空,則將禁用submit
,否則將啟用。
UPDATE
// Cache the submit button
var $submit = jQuery(".submit");
// Disable the button based on the dropdown value
$submit.prop("disabled", jQuery("#item_location").val() === "");
// Change the src image, based on disabled attribute of submit button
$submit.prop("src", function (i, val) {
return $submit.prop("disabled") ? 'old_image_src' : 'new_image_src';
});
嘗試這個。
jQuery("#item_location").change(function() {
if (this.value) {
jQuery(".submit").prop("src", "http://www.paypalobjects.com/en_US/i/btn/btn_buynow_LG.gif").removeAttr("disabled");
}
})
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.