Javascript 单选按钮选择列表框更改并将空值传递给每个列表框

Javascript 单选按钮选择列表框更改并将空值传递给每个列表框,javascript,Javascript,当单选按钮选择列表框更改并将空值传递给每个列表框…如空值或空白 其java脚本代码: <div> <input type="radio" name="rdo" id="rdo1" value="list1" />Radio1 <select id="select1" style='display:none;'> <option value="l1">1.1</option> <opt

当单选按钮选择列表框更改并将空值传递给每个列表框…如空值或空白

其java脚本代码:

<div>
    <input type="radio" name="rdo" id="rdo1" value="list1" />Radio1
    <select id="select1" style='display:none;'>
        <option value="l1">1.1</option>
        <option value="l2">2.2</option>
    </select>
</div>
<div>
    <input type="radio" name="rdo" id="rdo2" checked="checked" value="list2"/>Radio2
    <select id="select2">
        <option value="l1">2.1</option>
        <option value="l2">2.2</option>
    </select>
</div>

<script>
function select1() {
    document.getElementById("select1").style.display="block";
    document.getElementById("select2").style.display="none";   
}

function select2() {
 document.getElementById("select2").style.display="block";
document.getElementById("select1").style.display="none";             
}    


document.getElementById("rdo1").onclick = select1;
document.getElementById("rdo2").onclick = select2;
</script>

无线电1
1.1
2.2
无线电2
2.1
2.2
函数select1(){
document.getElementById(“select1”).style.display=“block”;
document.getElementById(“select2”).style.display=“无”;
}
函数select2(){
document.getElementById(“select2”).style.display=“block”;
document.getElementById(“select1”).style.display=“无”;
}    
document.getElementById(“rdo1”).onclick=select1;
document.getElementById(“rdo2”).onclick=select2;

我希望这是您所期望的

参见[jsFiddle][1]

[1] :。现在查看我的更新代码

function select1(someValue) {
        document.getElementById("select1").style.display = "block";
        var option = document.createElement("option");
        option.value = "";
        option.text = "";
        document.getElementById("select1").appendChild(option);
        document.getElementById("select2").style.display = "none";

    }

    function select2(someValue) {
        document.getElementById("select2").style.display = "block";
        document.getElementById("select1").style.display = "none";
        var option = document.createElement("option");
        option.value = "";
        option.text = "";
        document.getElementById("select2").appendChild(option);
    }
    document.getElementById("rdo1").onclick = function () {
        select1('someValue1');
    }
    document.getElementById("rdo2").onclick = function () {
        select2('someValue2');
    }
试试这个:

<div>
   <input type="radio" class="radiobtns" name="rdo" id="rdo1" value="list1" />Radio1
   <select id="select1" class="select_list" style='display:none;'>
      <option value="l1">1.1</option>
      <option value="l2">2.2</option>
   </select>
</div>
<div>
   <input type="radio" class="radiobtns" name="rdo" id="rdo2" checked="checked"   value="list2"/>Radio2
   <select id="select2" class="select_list">
      <option value="l1">2.1</option>
      <option value="l2">2.2</option>
   </select>
</div>

<script>

  $('.radiobtns').click(function(event){  
     var $ele = $(event.currentTarget),
     $select = $ele.siblings('.select_list');
     $('.select_list').hide();
     var empty_exists = $select.find('option').filter(function() {
        return this.value == null || this.value == '';
     });

     if(empty_exists.length == 0){
       $select.append('<option value=""></option>');
       $select.val('');     
     }
     $select.show();         
  }); 

</script>

无线电1
1.1
2.2
无线电2
2.1
2.2
$('.radiobtns')。单击(函数(事件){
var$ele=$(event.currentTarget),
$select=$ele.sibbins('.select_list');
$('.select_list').hide();
var empty_exists=$select.find('option').filter(函数(){
返回this.value==null | | this.value=='';
});
if(存在空_.length==0){
$select.append(“”);
$select.val(“”);
}
$select.show();
}); 

使用jquery和javascript查看演示

您希望从中得到什么。问题是,如果我选择第一个单选按钮,它会在列表框中填充一些值,当时我想像“Null value”一样将值传递给列表框…所以对于所有人来说…在u r脚本中,它将值传递给单选按钮onclick,但我必须插入“Null value”要在单击单选按钮时显示列表框,您可以使用此行
document.getElementById(“select1”).innerHTML=“”在你的函数里面。您可以指定空值。有解决办法吗?现在看答案。我更新了我的答案。这里我也把我修改过的代码放在这里<代码>变量选项=document.createElement(“选项”);option.value=“”;option.text=“”;document.getElementById(“select2”).appendChild(选项)不,我在做一个项目,我认为它只有这个支持。现在它创建了空空间…但单击单选按钮,空空间将被设置为默认值…如何实现?使用下面这行代码<代码>文档.getElementById(“select1”).value=“”。对于设置,选择框已选定