簡體   English   中英

切換禁用的單選按鈕並更改檢查值

[英]Toggling a disabled radio button and changing checked value

當從下拉列表中選擇一個選項時,我正在運行jQuery腳本以禁用單選按鈕。 同時,我要求另一個單選按鈕被選中。

禁用和重新啟用工作正常,但是checked屬性僅在第一次使用時有效。 而且只在第一台收音機上。

這是js代碼:

$(document).ready(function () {
// disable the no end date radio button if the selection is changed to lifetime:
        $("#budget_type").change(function () {
            if ($(this).val() === "lifetime_budget") {
                $('#schedule_no_end').attr("disabled", true);
                $('#run_on_schedule').attr("disabled", false);

                $('.schedule_end_radio').attr('checked',true);
            } else {

                $('#run_on_schedule').attr("disabled", true);
                $('#schedule_no_end').attr("disabled", false);

                $('.run_always').attr('checked',true);

            }
        });

    });
});

和HTML:

Budget: <select id="budget_type" name="budget_type">
    <option value="daily_budget">Daily budget</option>
    <option value="lifetime_budget">Lifetime budget</option>
</select>
Schedule End: <label>No End Date<input checked name="schedule_end" id="schedule_no_end" value="ongoing"
                                       type="radio"></label><br>
<label>End on:<input name="schedule_end" id="schedule_end_radio" class="schedule_end_radio" value="endondate" type="radio"></label>
<input class="datetimepicker" name="end_date" type="text"><br><br>

Advert Scheduling: <label>Run adverts all the time<input checked name="advert_scheduling" id="run_always" class="run_always" value="allthetime"
                                                         type="radio"></label>
<label>Run adverts on schedule<input name="advert_scheduling" id="run_on_schedule"  value="onschedule" type="radio" disabled></label>
  Try this
 $(document).ready(function () {
    $("#budget_type").change(function () {
        if ($(this).val() === "lifetime_budget") {
            $('#schedule_no_end').prop("disabled", true);
            $('#run_on_schedule').prop("disabled", false);

            $('.schedule_end_radio').prop('checked',true);
        } else {

            $('#run_on_schedule').prop("disabled", true);
            $('#schedule_no_end').prop("disabled", false);

            $('.run_always').prop('checked',true);

        }
    });

});

}); `

暫無
暫無

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

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