简体   繁体   English

单击单选按钮时不会禁用下拉菜单

[英]Dropdown not disable when radio button is clicked

It's supposed to be working but unfortunately it isn't.它应该可以工作,但不幸的是它没有。 I don't know what is wrong with my code.我不知道我的代码有什么问题。 When I try in jsfiddle , it's working but in my Notepad++ it doesn't works.当我在 jsfiddle 中尝试时,它可以工作,但在我的 Notepad++ 中它不起作用。 I can't figure out what's wrong with this.我无法弄清楚这有什么问题。

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

The issue is not with the code.问题不在于代码。 The issue is with the way you are referencing the libraries .问题在于您引用库的方式 Change the references to have a https://将引用更改为具有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>

It is not working in your Notepad++ because you are viewing this file in the brwoser which will have the file:// request and not https:// .它在您的 Notepad++ 中不起作用,因为您正在 brwoser 中查看此文件,该文件将具有file://请求而不是https:// It works in your fiddle because there you might be referencing the library in different way but Valid way .它适用于您的小提琴,因为您可能以不同的方式引用库,但Valid way

Also pointing out a mistake in your code.还指出您的代码中的错误。

This line $('#seniordis').removeAttr('disabled', true);这一行$('#seniordis').removeAttr('disabled', true);

Here seniordis in the value of name attribute of your select tag and not the id if it... so use Either $('[name="seniordis"]') to target the element by name or $('#select') to target the element by id.这里seniordis在你的选择标签的name属性的值中,而不是id如果它......所以使用$('[name="seniordis"]')按名称或$('#select')定位元素按 id 定位元素。

So the code should be所以代码应该是

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

OR this或这个

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

Your problem is, senoirdis is name not a id.你的问题是, senoirdis是名字而不是 id。 Your id is select .您的 ID 是select

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

});

There are some Errors with your code您的代码有一些错误

  1. This line这条线

    input:radio[name="senior"]

    Must be this一定是这个

    input[name=senior]:radio
  2. This Line这条线

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

    Must be必须是

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

Use code Below:使用以下代码:

<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);
    }
});

jsFiddle js小提琴

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM