Javascript条件 - 取消选中隐藏div中的单选按钮?

时间:2013-04-25 22:50:54

标签: javascript radio clear

我正在尝试为表单设置条件类型单选按钮,我实际上修改了一些我在这里找到的javascript,如下所示:

function hideStuff(id) {
    document.getElementById('flatDiv').style.display = 'none';
    document.getElementById('salaryDiv').style.display = 'none';
}

function checkType(selectedType) {

    if (selectedType == 'flat') {
        document.getElementById('flatDiv').style.display = 'block';
        document.getElementById('salaryDiv').style.display = 'none';
    } else {
        document.getElementById('flatDiv').style.display = 'none';
        document.getElementById('salaryDiv').style.display = 'block';
    }
}

并且html是这样的:

<input type="radio" name="Type" value="flat" onclick="checkType(this.value)" />Flat
<input type="radio" name="Type" value="salary" onclick="checkType(this.value)" />Salaray
<br />
<div id="salaryDiv" style="display:none;">Choose an option:
    <input type="radio" name="salaryType" value="1x" />1x
    <input type="radio" name="salaryType" value="2x" />2x
    <input type="radio" name="salaryType" value="3x" />3x</div>
<div id="flatDiv" style="display:none;">Choose an option:
    <input type="radio" name="flatType" value="$25,000" />$25,000
    <input type="radio" name="flatType" value="$50,000" />$50,000</div>

jsFiddle

这一切都有效,但如果有人点击Salary并选择一个选项然后改变主意,当他们点击Flat时我想清除他们之前根据Salary做出的任何选择(反之亦然)我试过了一些不起作用的东西,所以我想我会问一个知道他们在做什么的人。感谢。

4 个答案:

答案 0 :(得分:2)

创建了功能clearRadio,并将其分配给具有flat属性的广播输入salaryonchange

HTML

<input type="radio" name="Type" value="flat" onclick="checkType(this.value)" onchange="clearRadios('flatDiv')" />Flat
    <input type="radio" name="Type" value="salary" onclick="checkType(this.value)" onchange="clearRadios('salaryDiv')" />Salary

的JavaScript

function clearRadios(id) {
    var Radios = document.getElementById(id).getElementsByTagName('input');
    for (var i = 0; i < Radios.length; i++) {
        if (Radios[i].type == 'radio') {
            Radios[i].checked = false;
        }
    }
}

该函数将使用传递的id找到div中的所有无线电输入并清除它们。

jsfiddle

答案 1 :(得分:1)

我建议使用Javascript库,但我离题了。我已修改您的代码以包含addEvent,而不是在标记中分配事件处理程序。它看起来更干净。您可以使用它。

(function () {
    var inputs = document.getElementsByTagName('input');
    for (var i = 0; i < inputs.length; i++) {
        if (inputs[i].getAttribute('name') === "Type") {
            addEvent('click', inputs[i], function (e) {
                var source = event.target || event.srcElement;
                checkType(source.value);
            });
        }
    }

    function hideStuff(id) {
        document.getElementById('flatDiv').style.display = 'none';
        document.getElementById('salaryDiv').style.display = 'none';
    }

    // What you had before but now it clears the radios of the other type
    function checkType(selectedType) {
        if (selectedType == 'flat') {
            clear("salaryType");
            document.getElementById('flatDiv').style.display = 'block';
            document.getElementById('salaryDiv').style.display = 'none';
        } else {
            clear("flatType");
            document.getElementById('flatDiv').style.display = 'none';
            document.getElementById('salaryDiv').style.display = 'block';
        }
    }

    function clear(str) {
        for (var i = 0; i < inputs.length;i++) {
            if (inputs[i].getAttribute('name') === str) {
                inputs[i].checked = false;
            }
        }
    }
})(); 
function addEvent(evnt, elem, func) {
   if (elem.addEventListener)  // W3C DOM
      elem.addEventListener(evnt,func,false);
   else if (elem.attachEvent) { // IE DOM
      elem.attachEvent("on"+evnt, func);
   }
   else { // No much to do
      elem[evnt] = func;
   }
}

所有内容都包含在一个匿名函数中,以减少全局变量。 addEvent函数允许跨浏览器添加动态事件侦听器。

此JavaScript对应于以下标记:

<input type="radio" name="Type" value="flat" id="flatRadio" />Flat
<input type="radio" name="Type" value="salary" id="salaryRadio" />Salary
<br />
<div id="salaryDiv" class="options">Choose an option:
    <input type="radio" name="salaryType" value="1x" />1x
    <input type="radio" name="salaryType" value="2x" />2x
    <input type="radio" name="salaryType" value="3x" />3x
</div>
<div id="flatDiv" class="options">Choose an option:
    <input type="radio" name="flatType" value="$25,000" />$25,000
    <input type="radio" name="flatType" value="$50,000" />$50,000
</div>

我还删除了内联样式,并将名为options的CSS类设置为display:none

.options {
    display:none;
}

jsFiddle

答案 2 :(得分:0)

我使用的技术是在所选选项中添加“标志”类(即虚拟类)。这使您能够为所选选项设置样式,并且可以轻松清除它。

  1. CSS
  2. #flatDiv, #salaryDiv{display: block;}
    .selected{ display: none; }
    
    1. 的javascript

      function hideStuff(id) {
        document.getElementById('flatDiv').className ="selected";
        document.getElementById('salaryDiv').className ="selected";
      }
      
      function checkType(selectedType){
          var fd = document.getElementById('flatDiv');
        var sd = document.getElementById('salaryDiv');
      
        if (selectedType == 'flat'){
            fd.className = ""; // clear it
            sd.className = "selected";
        } else{
            fd.className = "selected";
            sd.className = ""; // clear it
        }
      }
      

答案 3 :(得分:0)

我建议:

function hideStuff(elClassName) {
    // call the function to hide specific elements (based on a class-name)
    var els = document.getElementsByClassName(elClassName);
    for (var i = 0, len = els.length; i < len; i++){
        // iterate through those elements and hide them
        els[i].style.display = 'none';
    }
}

function checkType(selectedType) {
    var toggles = document.getElementsByClassName('toggle'),
        inputs;
    for (var i = 0, len = toggles.length; i < len; i++){
        // sets the current node's display to 'block' if the id matches, and 'none' if not
        toggles[i].style.display = toggles[i].id == selectedType + 'Div' ? 'block' : 'none';
        if (toggles[i].style.display == 'none') {
            // if an element is hidden, the radios therein are all unchecked
            inputs = toggles[i].getElementsByTagName('input');
            for (var c = 0, cLen = inputs.length; c < cLen; c++){
                inputs[c].checked = false;
            }
        }
    }
}

hideStuff('toggle');

JS Fiddle demo

当然,这是基于略微修改的HTML,在这种情况下使用类来标识相关元素,尽管可以使用data-*属性。

遗憾的是,所有浏览器都不支持

document.getElementsByClassName(),特别是在版本9之前,Internet Explorer不支持它(according to MDNQuirksmode)。

相关问题