簡體   English   中英

.prop('selected')不起作用,jquery中始終禁用按鈕

[英].prop('selected') not working, button is disabled all the time in jquery

一旦選擇了選擇字段中的狀態,我想啟用提交按鈕。

<div class="close_req">
    <div class="req_status">
        <select id="status_update">
            <option value="status" selected="selected" disabled="disabled">Status</option>
            <option value="complete">Complete</option>
            <option value="pending">Pending</option>
        </select>
    </div>
    <div class="req_addcharges">
        <select id="additional_charges">
            <option value="charge">Additional Charges</option>
            <option value="complete">Yes</option>
            <option value="pending">No</option>
        </select>
    </div>
    <div class="additional_charge">
        <input type="text" id="add_price" name="add_price" placeholder="Enter the additional charge" />
    </div>
    <div class="task_desc">
        <textarea id="task" name="task" cols="10" rows="6" placeholder="Description of the task"></textarea>
    </div>
    <div class="task_complete">
        <input type="submit" id="complete" name="complete" value="Task Complete" />
    </div>
    </form>
</div>

這是我嘗試過的jQuery腳本

$(document).ready(function () {
    $("#complete").attr("disabled", "disabled");

    function updateFormEnabled() {
        if ($("#status_update").prop('selected', true)) {
            $("#complete").removeAttr('disabled');
        } else {
            $("#complete").attr('disabled', 'disabled');
        }
    }
});
$("#status_update").change(updateFormEnabled);

但是提交按鈕始終處於禁用狀態,我希望一旦在選擇字段中選擇了狀態后就啟用該按鈕。 誰能幫我解決這個問題。

您的病情很嚴重,應該是

if ($("#status_update").prop('selected') == true)

$("#status_update").change(updateFormEnabled); 應該在$(document).ready(function () {});

#status_update是選擇列表,而不是選項,您必須檢查選中的選項:

function updateFormEnabled() {
        $("#complete").attr('disabled', 'disabled');
        $('#status_update option').each(function() {
           if($(this).is(':selected')){
               $("#complete").removeAttr('disabled');
               return;
           }
        }
    }
$("#status_update").change(updateFormEnabled);

另一種方法是

if($("#status_update option:selected").length) {/* do action */}

JSFiddle

http://jsfiddle.net/9pqekb63/1/

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

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