簡體   English   中英

單擊單選按鈕時不會禁用下拉菜單

[英]Dropdown not disable when radio button is clicked

它應該可以工作,但不幸的是它沒有。 我不知道我的代碼有什么問題。 當我在 jsfiddle 中嘗試時,它可以工作,但在我的 Notepad++ 中它不起作用。 我無法弄清楚這有什么問題。

<head>
 <link rel="stylesheet" href="//code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css">
  <script src="//code.jquery.com/jquery-1.10.2.js"></script>
  <script src="//code.jquery.com/ui/1.11.4/jquery-ui.js"></script>
<script type="text/javascript">

 $('input:radio[name="senior"]').change(function() {
        if ($(this).val()=='Yes') {
            $('#seniordis').attr('disabled',false);
        } else


            $('#seniordis').removeAttr('disabled', true);
    });
    </script>



</head>



Senior : 

<input name="senior" type="radio" id="Yes" value="Yes" />Yes
<input name="senior" type="radio" id="No" value="No" selected="selected" />No<br />  
<select name="seniordis" id="select">
<option value="100" >100% discount</option>
<option value="50">50% discount</option>
<option value="10">10% discount</option>
</select>                  
</td>

問題不在於代碼。 問題在於您引用庫的方式 將引用更改為具有https://

<link rel="stylesheet" href="https://code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css">
<script src="https://code.jquery.com/jquery-1.10.2.js"></script>
<script src="https://code.jquery.com/ui/1.11.4/jquery-ui.js"></script>

它在您的 Notepad++ 中不起作用,因為您正在 brwoser 中查看此文件,該文件將具有file://請求而不是https:// 它適用於您的小提琴,因為您可能以不同的方式引用庫,但Valid way

還指出您的代碼中的錯誤。

這一行$('#seniordis').removeAttr('disabled', true);

這里seniordis在你的選擇標簽的name屬性的值中,而不是id如果它......所以使用$('[name="seniordis"]')按名稱或$('#select')定位元素按 id 定位元素。

所以代碼應該是

$('#select').attr('disabled', true);

或這個

$('#select').removeAttr('disabled');

你的問題是, senoirdis是名字而不是 id。 您的 ID 是select

$().ready(function()
{
    $('input:radio[name="senior"]').change(function() {
        if ($(this).val()=='Yes') {
            $('#select').prop('disabled',false);
        } 
        else
        {
            $('#select').prop('disabled',true);
        }
    });

});

您的代碼有一些錯誤

  1. 這條線

    input:radio[name="senior"]

    一定是這個

    input[name=senior]:radio
  2. 這條線

    $('#seniordis').attr('disabled',false);

    必須是

    $('#select:input').removeAttr('disabled');

使用以下代碼:

<div>
    <input name="senior" type="radio" id="Yes" value="Yes"  />Yes
    <input name="senior" type="radio" id="No" value="No" />No<br />
    <select name="seniordis" id="select">
        <option value="100">100% discount</option>
        <option value="50">50% discount</option>
        <option value="10">10% discount</option>
    </select>
</div>
<script type="text/javascript">
    $("input[name=senior]:radio").change(function () {
        if ($(this).val() == "Yes") {
            $('#select:input').removeAttr('disabled');
        }
        else {
            $('#select:input').attr('disabled', 'disabled');
        }
    });
</script>
$('input:radio[name="senior"]').change(function() {
    if ($(this).val() == 'Yes') {
        $('select[name="seniordis"]').attr('disabled',false);
    } else {
        $('select[name="seniordis"]').attr('disabled', true);
    }
});

js小提琴

暫無
暫無

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

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