使用 javascript 选择选择框

发布于 2024-09-26 12:32:29 字数 790 浏览 2 评论 0原文

我有一个关于使用 Javascript 选择选择框的问题。我已将文件上传到 http://www.mikevierwind.nl/javascript/risicoinventarisatie.htm 这里你会看到一个有很多问题和单选按钮的表格。每个问题都有 2 个单选按钮,是和否。

当你问第一个问题时。 “糖尿病”比您选择的单选按钮“ja”您看到现在将显示另一个问题。但当我选择“nee”时,这个问题就消失了。我怎样才能用Javascript创建它?我的 JavaScript 代码是:

 var diabetes = false;
 var diabetesvraag = $("#blokken table.lichamelijkegezondheid .diabetesextra")
 var buttondiabetes = $("#blokken table.lichamelijkegezondheid .diabetesja")

 diabetesvraag.hide();
 buttondiabetes.click(function()
 {   
  if(diabetes == false)
  {
   diabetes = true;
   diabetesvraag.show();
  }

  else
  {
   diabetes = false;
   diabetesvraag.hide();
  }
  return false;
 }); 

I have a question about selecting selectboxes with Javascript. I have upload the files on http://www.mikevierwind.nl/javascript/risicoinventarisatie.htm Here you see a table with a lot of questions and radiobuttons. Every question have 2 radiobuttons, yes and no.

When you go to the first question. "Diabetes" Than you choose for the radiobutton "ja" You see that another question will now show. But when I choose for "nee" than the question must be gone. How can I create that with the Javascript? My Javascript code is:

 var diabetes = false;
 var diabetesvraag = $("#blokken table.lichamelijkegezondheid .diabetesextra")
 var buttondiabetes = $("#blokken table.lichamelijkegezondheid .diabetesja")

 diabetesvraag.hide();
 buttondiabetes.click(function()
 {   
  if(diabetes == false)
  {
   diabetes = true;
   diabetesvraag.show();
  }

  else
  {
   diabetes = false;
   diabetesvraag.hide();
  }
  return false;
 }); 

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

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

发布评论

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

评论(2

何其悲哀 2024-10-03 12:32:29

好吧,首先您需要更正您的 HTML。每组“是”和“否”单选按钮都需要使用正确的 name 属性分组在一起。这将告诉浏览器每组“是”和“否”中只能选择一个。但是,每个条件的名称必须是唯一的。应该这样做:

<span class="radio">
    <input type="radio" name="condition_selection" value="yes" id="yes" />
    <label for="yes">Yes</label>
    <input type="radio" name="condition_selection" value="no" id="no" />
    <label for="no">No</label></span>

出于可访问性原因,您还应该使用 label 元素的 for 属性,并尝试使用除表格之外的其他内容来标记此表单,但是这与这里的问题无关。


接下来,您可以尝试使用 change 事件处理程序来检查这两种状态来简化 Javascript 代码,如下所示:

$('.add_qns').hide();

$('.radio :radio').change(function(){
    if(this.value === 'yes'){
        $(this).parent().next('.add_qns').show();
    } else {
        $(this).parent().next('.add_qns').hide();
    }
});

您可以更改选择器 $(this).parent()。 next('.add_qns') 以适合您的标记。请参阅:http://www.jsfiddle.net/yijian/BcAwH/ 进行现场演示。

Well, first of all you need to get your HTML corrected. Each set of "yes" and "no" radiobuttons need to be grouped together with the correct name attribute. This will tell the browser that only one of each set of "yes" and "no" is selectable. The name must be unique for each condition, however. Something like this should do:

<span class="radio">
    <input type="radio" name="condition_selection" value="yes" id="yes" />
    <label for="yes">Yes</label>
    <input type="radio" name="condition_selection" value="no" id="no" />
    <label for="no">No</label></span>

You should also use the for attribute for the label element for accessibility reasons, and try using something else other than tables for marking up this form, but that's not relevant to the question here.


Next, you can try simplifying the Javascript code using the change event handler to check for both states, with something like this:

$('.add_qns').hide();

$('.radio :radio').change(function(){
    if(this.value === 'yes'){
        $(this).parent().next('.add_qns').show();
    } else {
        $(this).parent().next('.add_qns').hide();
    }
});

You have the change the selector $(this).parent().next('.add_qns') to suit your markup. See: http://www.jsfiddle.net/yijiang/BcAwH/ for a live demo.

高冷爸爸 2024-10-03 12:32:29

class="diabetesja" 也添加到 nee 按钮,因为您的代码仅将点击事件分配给 .diabetesja 单选按钮。

或者,您可以添加一个类class="diabetesnee" 到您的 nee 单选按钮并将其更改

var buttondiabetes = $("#blokken table.lichamelijkegezondheid .diabetesja")

var buttondiabetes = $("#blokken table.lichamelijkegezondheid .diabetesja, #blokken table.lichamelijkegezondheid .diabetesnee")

要点是两个单选按钮(Ja 和 Nee)都需要有点击处理程序。

Put the class="diabetesja" to the nee buttons as well, as your code only assigns the click event to the .diabetesja radio buttons..

Alternatively you could add a class class="diabetesnee" to your nee radio buttons and change the

var buttondiabetes = $("#blokken table.lichamelijkegezondheid .diabetesja")

to

var buttondiabetes = $("#blokken table.lichamelijkegezondheid .diabetesja, #blokken table.lichamelijkegezondheid .diabetesnee")

The point is that both radio buttons (Ja and Nee) need to have the click handler..

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