如何使用jQuery根据单选按钮是否选中显示内容

发布于 2024-08-15 16:40:58 字数 745 浏览 4 评论 0原文

我被某件事困住了。我有一个表单,其中的字段仅在选中特定单选按钮时显示。当有人第一次使用以下代码填写表单时,这很容易。

HTML

<fieldset id="question">
  <legend>This is my question</legend>
  <label for="answerYes">Yes</label>
  <input name="answer" id="answerYes" type="radio" value="1" />
  <label for="answerNo">No</label>
  <input name="answer" id="answerNo" type="radio" value="0" />
</fieldset>

jQuery

$("#subQuestion").hide();
$("#answerYes").click(function() {
  $("#subQuestion").show();
});
$("#answerNo").click(function() {
  $("#subQuestion").hide();
});

当有人返回表单进行编辑时,此逻辑就会崩溃。在这种情况下,字段已预先填充,因此单击功能不存在。如果在页面加载时选中#answerYes,我如何使用 jQuery 显示#subQuestion。

I'm stuck on something. I have a form that has fields which only show when a particular radio button is checked. This is easy enough to when someone is filling out the form for the first time using the following code.

HTML

<fieldset id="question">
  <legend>This is my question</legend>
  <label for="answerYes">Yes</label>
  <input name="answer" id="answerYes" type="radio" value="1" />
  <label for="answerNo">No</label>
  <input name="answer" id="answerNo" type="radio" value="0" />
</fieldset>

jQuery

$("#subQuestion").hide();
$("#answerYes").click(function() {
  $("#subQuestion").show();
});
$("#answerNo").click(function() {
  $("#subQuestion").hide();
});

This logic breaks down though when someone comes back to the form to edit it. In that case, the fields are prepopulated so the click function doesn't exist. How can I use jQuery to show #subQuestion if #answerYes is checked when the page loads.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

南街女流氓 2024-08-22 16:40:58

尝试在页面中添加 $(document).ready() 处理程序...

$(document).ready(function(){
  // do your checks of the radio buttons here and show/hide what you want to
  $("#subQuestion").hide();
  if ($("#answerYes:checked").length > 0){ $("#subQuestion").show(); }

  // add functionality for the onclicks here
  $("#answerYes").click(function() {
    $("#subQuestion").show();
  });

  $("#answerNo").click(function() {
    $("#subQuestion").hide();
  });
});

一旦页面完全加载(并动态预填充值),该处理程序就会触发

Try adding a $(document).ready() handler to your page...

$(document).ready(function(){
  // do your checks of the radio buttons here and show/hide what you want to
  $("#subQuestion").hide();
  if ($("#answerYes:checked").length > 0){ $("#subQuestion").show(); }

  // add functionality for the onclicks here
  $("#answerYes").click(function() {
    $("#subQuestion").show();
  });

  $("#answerNo").click(function() {
    $("#subQuestion").hide();
  });
});

This handler will fire off once the page is completely loaded (and dynamically pre-populated with values)

我不是你的备胎 2024-08-22 16:40:58

将逻辑附加到 change() 事件*。此外,您可以使用 :checked 选择器来获取当前选定的单选按钮。

<input name="food" type="radio" value="apples" />
<input name="food" type="radio" value="oranges" />

--

$(":radio[name='food']").change(function(){
  var newVal = $(":radio[name='food']:checked").val();
  if (newVal == "apples") {
    $("#someBox").show();
  } else {
    $("#someBox").hide();
  }
});

* 有些人报告了更改的问题,并改为
使用click()来处理逻辑

Attach the logic to the change() event*. Furthermore, you can use dthe :checked selector to get the currently-selected radio button.

<input name="food" type="radio" value="apples" />
<input name="food" type="radio" value="oranges" />

--

$(":radio[name='food']").change(function(){
  var newVal = $(":radio[name='food']:checked").val();
  if (newVal == "apples") {
    $("#someBox").show();
  } else {
    $("#someBox").hide();
  }
});

* Some have reported isues with change, and have instead
used click() to handle the logic

許願樹丅啲祈禱 2024-08-22 16:40:58

将所有渲染逻辑保留在单个函数中,并在文档返回时调用该函数:

function renderSubquestion() {
  $('#subQuestion').toggle($("#answerYes").attr('checked'));
}

$(function(){
  $('#answerNo').click(renderSubquestion);
  $('#answerYes').click(renderSubquestion);
  renderSubquestion();
}

Keep all the rendering logic in a single function, and call that function when the document comes back:

function renderSubquestion() {
  $('#subQuestion').toggle($("#answerYes").attr('checked'));
}

$(function(){
  $('#answerNo').click(renderSubquestion);
  $('#answerYes').click(renderSubquestion);
  renderSubquestion();
}
逆光下的微笑 2024-08-22 16:40:58

如果答案在服务器端,例如通过 PHP,那么您应该使用服务器端代码设置 #subQuestion 的样式:

if($databaseFieldForAnswer != 1){
    $subDiv = '<div style=\"visibility: hidden\"></div>';
}

...更好的是,分配一个类来处理是否显示 div。

If the answer is on the server side, like via PHP, then you should just set the style of #subQuestion using your server side code:

if($databaseFieldForAnswer != 1){
    $subDiv = '<div style=\"visibility: hidden\"></div>';
}

...better yet, assign a class that handles whether the div is shown or not.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文