繁体   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