【问题标题】:Restrict radio button display, javascript限制单选按钮显示,javascript
【发布时间】:2014-03-14 02:35:45
【问题描述】:

我有一个下拉列表。单击其中一项时,应显示单选按钮以显示。

我到了这里。现在的问题是

我有 4 个单选按钮。我想显示 2 个选项并隐藏 2 个选项。在javascript中。

我该怎么做?

【问题讨论】:

  • 你能添加一个链接到你的代码的jsfiddle吗?
  • 请帮助您的 html 代码。将您的代码添加到 www.jsfiddle.net 并在此处分享链接。谢谢
  • 放到jsfiddle.net(这是一个js评估的网站),这样每个人都可以运行你的代码。更容易讨论和评估。
  • 我不熟悉 jsfiddle

标签: javascript button radio


【解决方案1】:

你可以用 jquery 做到这一点

$(function() {
    $('#dropdownId').change(function() { //when the dropdown change
        var selectValue = $(this).val(); //Get it's value
        if (selectValue == 1) {
            $("#radioButtonId1").show();
            $("#radioButtonId2").show();
            $("#radioButtonId3").hide();
            $("#radioButtonId4").hide();
        }
    });
}

您还可以使用选项中的数据来获取需要显示的选项:

<input type = "radio" class="DependOnSelect" data-showonid="1,3,5" />
<input type = "radio" class="DependOnSelect" data-showonid="2,4,6" />
<input type = "radio" class="DependOnSelect" data-showonid="1,4,6" />
<input type = "radio" class="DependOnSelect" data-showonid="2,3,5" />

$(function() {
    $('#dropdownId').change(function() {
        var selectValue = $(this).val();
        if (selectValue == 1) {
            $('.DependOnSelect').each(function() { //for each Options
                var splited = $(this).data("showonid").split(','); 
                if (jQuery.inArray(selectValue, splited) > -1)
                    $(this).show(); // if the data-showonid contains the id, show it
                else
                    $(this).hide();                    
            });
        }
    });
}

JSFIDLE 禁用 jquery 中的元素:

$('#ElementId').attr("disabled", "disabled);

启用它:

$('#ElementId').removeAttr("disabled");

纯 JavaScript:

HTML:

<select id="dropdownId" onChange="jsFunction()">
    <option value='1'>1</option>
    <option value='2'>2</option>
    <option value='3'>3</option>
    <option value='4'>4</option>
</select>
<br/>
<input type="radio" name="option" id="option1" />1
<br/>
<input type="radio" name="option" id="option2" />2
<br/>
<input type="radio" name="option" id="option3" />3
<br/>
<input type="radio" name="option" id="option4" />4
<br/>

javascript:

function jsFunction() {
    var myselect = document.getElementById("dropdownId");
    var value = myselect.options[myselect.selectedIndex].value;

    if (value == 1) {
        document.getElementById('option1').disabled = 'disabled';
    } else {
        document.getElementById('option1').disabled = '';
    }

    if (value == 2) {
        document.getElementById('option2').disabled = 'disabled';
    } else {
        document.getElementById('option2').disabled = '';
    }

    if (value == 3) {
        document.getElementById('option3').disabled = 'disabled';
    } else {
        document.getElementById('option3').disabled = '';
    }

    if (value == 4) {
        document.getElementById('option4').disabled = 'disabled';
    } else {
        document.getElementById('option4').disabled = '';
    }
}

JSFIDLE

【讨论】:

  • 你能用javascript告诉我吗?不是 jquery。
  • 如果你有相同 id 的对象,我不认为它可以被制作...你有你的代码的 jsfidle 吗?
  • 是的,但我需要这个 javascript。我的构建不允许 jquery
  • 如果它是您要查找的内容,请不要忘记将其标记为已回答 :)
【解决方案2】:

您好,您可以使用“禁用”禁用任何特定选项

<input type="radio" name="someName" value="someValue" id="someId" disabled>

Then get the two options you want to hide and make display = "none";

【讨论】:

  • 我需要使用 javascript 禁用。
  • document.getElementById("someId").disabled=true;
  • 所有选项都有相同的id
  • 但每个选项都有不同的价值。我想限制单选按钮 3、单选按钮 4
  • "id" 不应该相等。对于单选按钮,“名称”将是相同的。
猜你喜欢
相关资源
最近更新 更多
热门标签