簡體   English   中英

如果選擇框選項具有值,則禁用禁用的屬性

[英]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';
});

使用.val()獲取下拉列表的選定值

if (jQuery("#item_location").val() !== "") {
                             ^           ^ //added ) here

或更好地使用.prop()

jQuery(".submit").prop("disabled", jQuery("#item_location").val() === "")

閱讀.prop()與.attr()


OP評論后更新

 jQuery(".submit").prop("src", "http://www.paypalobjects.com/en_US/i/btn/btn_buynow_LG.gif"); 

嘗試這個。

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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM