如果选中复选框,则仅选择一个复选框(并取消选择其余)并显示输入字段

时间:2011-05-07 12:21:47

标签: php javascript jquery codeigniter checkbox

  1. 我有一个带有“是”和“否”的表单作为两个复选框。这些一开始就没有被选中。我希望用户只选择其中一个,如果他们不想做出决定,可以取消选择他们的选择(这就是为什么我没有使用单选按钮)。选择是应取消选择否,反之亦然。我不知道该怎么做。我正在使用PHP(codeigniter),Javascript和JQuery。

  2. 其次,选择“是”或“否”后,需要显示输入字段。我有这个设置,但它切换“onclick”动作,这意味着选择是两次显示并隐藏输入字段!我希望输入字段显示是否选择是或否,如果取消选择是和否,则表示消失。

    $do_toggle = "onClick=\"javascript:toggle('togglenote');\"";
    
    echo form_radio('decision'.$key,'y',FALSE,$do_toggle)."Yes ";
    
    echo form_radio('decision'.$key,'n',FALSE,$do_toggle)."No";
    
    echo form_input($mytext);
    
    // I put a div tag around the form_input above
    // but it's not showing in the StackOverflow question...
    // but it's there for the toggle to work.
    

3 个答案:

答案 0 :(得分:0)

假设你的html是这样的(我认为是):

<input type="checkbox" name="decisionY" value="y" /> Yes
<input type="checkbox" name="decisionN" value="N" /> Yes
<input type="text" id="togglenote" name="togglenote" />

你的js会是:

$(document).ready(function(){
  $(":checkbox[name^='decision']").change(function(){
     if($(this).is(":checked")){
        $(":checkbox[name[^='decision']").attr("checked", false); //Uncheck the other
        $(this).attr("checked", true);
        $("#togglenote").show();
     }
     if($(":checkbox[name^='decision']:checked").size() == 0){
        $("#togglenote").hide();
     }
  });
});

希望这会有所帮助。欢呼声。

答案 1 :(得分:0)

这应该做你想要的:

$("#checkbox1").change(function() {

if ($(this).attr("checked") && $("#checkbox2").attr("checked")) {
   $("checkbox2").removeAttr("checked");
} else if ($(this).attr("checked")) {
   $("#inputfield").show();
} else {
   $("#inputfield").hide();
}
});


$("#checkbox2").change(function() {

if ($(this).attr("checked") && $("#checkbox1").attr("checked")) {
   $("checkbox1").removeAttr("checked");
} else if ($(this).attr("checked")) {
   $("#inputfield").show();
} else {
   $("#inputfield").hide();
}
});

答案 2 :(得分:0)

  1. 选择其中一个作为jQuery或javascript
  2. 不要认为这是100%保证一些黑客无法同时设置和发布:)
  3. Analise $ _REQUEST数组反对设置是/否值
  4. 做出决定。
  5. PS。 JQuery不能在我的诺基亚6303c :)设备上工作,硬编码 - 会起作用......所以应用程序必须有2个部分。

相关问题