簡體   English   中英

僅選擇一個復選框(並取消選擇休息)並在選中復選框時顯示輸入字段

[英]Select only one checkbox (and deselect rest) and show input field if checkbox selected

  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.

假設你的 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();
     }
  });
});

希望這可以幫助。 干杯。

這應該做你想做的:

$("#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();
}
});
  1. 選擇其中之一作為 jQuery 或 javascript
  2. 不要認為這是 100% 保證某些黑客不能同時設置和發布:)
  3. 針對設置的是/否值分析 $_REQUEST 數組
  4. 做出決定。

附注。 JQuery 不適用於我的 nokia 6303c :) 設備,硬編碼 - 會工作......所以應用程序必須有 2 個部分。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM