[英]Multiple text field, show/hide based on radio selection (Html,JS)
I am working on a form
right now and have the following problem.我现在正在处理一个form
,但遇到以下问题。
Problem问题
I have multiple questions on the same page with the exact same answer options (yes/no/maybe), and one of them is "maybe".我在同一页面上有多个问题,答案选项完全相同(是/否/也许),其中之一是“也许”。 After the user has checked a radio-button
or a check-box
called "maybe" a text field
should appear.在用户选中一个radio-button
或一个名为“可能”的check-box
应该会出现一个text field
。 I have implemented a javascript but it doesn't seem to work.我已经实现了一个 javascript,但它似乎不起作用。 I don't really know how to program this when there are multiple text fields and radio buttons.当有多个文本字段和单选按钮时,我真的不知道如何编程。
I am fairly new to HTML/Jquery/Javascript/CSS.我对 HTML/Jquery/Javascript/CSS 相当陌生。
How can this be achieved?如何做到这一点? I know similar questions have been asked, but I am really clueless at this point.我知道有人问过类似的问题,但在这一点上我真的一无所知。
Thank you very much for your support!非常感谢您的支持!
[EDIT] I tried giving the the same id's but it doesn't seem to work. [编辑] 我尝试提供相同的 ID,但它似乎不起作用。
Here is a fiddle with the code: http://jsfiddle.net/03gkgfah/1/这是代码的小提琴: http : //jsfiddle.net/03gkgfah/1/
This is an anonymized excerpt from the form (i didn't simply it on purpose so you could have a better overview):这是表格的匿名摘录(我不是故意简单地这样做,所以你可以有一个更好的概述):
<fieldset data-mini="false" data-inline="true">
<legend>1. Test question 1</legend>
<div class="ui-grid-b ui-responsive">
<div class="ui-block-a">
<input name="Teil01_Frage01" id="tUmfrage01_p01_f01_q01_radio01" value="yes" type="radio">
<label for="tUmfrage01_p01_f01_q01_radio01">Yes</label>
</div>
<div class="ui-block-b">
<input name="Teil01_Frage01" id="tUmfrage01_p01_f01_q01_radio02" value="no" type="radio">
<label for="tUmfrage01_p01_f01_q01_radio02">No</label>
</div>
<div class="ui-block-a">
<input name="Teil01_Frage01" id="tUmfrage01_p01_f01_q01_radio03" value="maybe" type="radio">
<label for="tUmfrage01_p01_f01_q01_radio03">Maybe:</label>
</div>
<div class="ui-block-b" id="maybe_on">
<input name="Teil01_Frage01" id="tUmfrage01_p01_f01_q01_textinput01" value="" type="text" maxlength="150" placeholder="maybe...">
</div>
</div>
</fieldset>
<fieldset data-mini="false" data-inline="true">
<legend>2. Test question 2?</legend>
<div class="ui-grid-b ui-responsive">
<div class="ui-block-a">
<input name="Teil01_Frage02" id="tUmfrage01_p01_f01_q02_radio01" value="yes" type="radio">
<label for="tUmfrage01_p01_f01_q02_radio01">Yes</label>
</div>
<div class="ui-block-b">
<input name="Teil01_Frage02" id="tUmfrage01_p01_f01_q02_radio02" value="no" type="radio">
<label for="tUmfrage01_p01_f01_q02_radio02">No</label>
</div>
<div class="ui-block-a">
<input name="Teil01_Frage02" id="tUmfrage01_p01_f01_q02_radio03" value="maybe" type="radio">
<label for="tUmfrage01_p01_f01_q02_radio03">Maybe:</label>
</div>
<div class="ui-block-b" id="maybe_on">
<input name="Teil01_Frage02" id="tUmfrage01_p01_f01_q02_textinput01" value="" type="text" maxlength="150" placeholder="maybe...">
</div>
</div>
</fieldset>
<fieldset data-mini="false" data-inline="true">
<legend>2. Test question 3?</legend>
<div class="ui-grid-b ui-responsive">
<div class="ui-block-a">
<input name="Teil01_Frage03" id="tUmfrage01_p01_f01_q03_radio01" value="yes" type="radio">
<label for="tUmfrage01_p01_f01_q03_radio01">Yes</label>
</div>
<div class="ui-block-b">
<input name="Teil01_Frage03" id="tUmfrage01_p01_f01_q03_radio02" value="no" type="radio">
<label for="tUmfrage01_p01_f01_q03_radio02">No</label>
</div>
<div class="ui-block-a">
<input name="Teil01_Frage03" id="tUmfrage01_p01_f01_q03_radio03" value="maybe" type="radio">
<label for="tUmfrage01_p01_f01_q03_radio03">Maybe:</label>
</div>
<div class="ui-block-b" id="maybe_on">
<input name="Teil01_Frage03" id="tUmfrage01_p01_f01_q03_textinput01" value="" type="text" maxlength="150" placeholder="maybe...">
</div>
</div>
</fieldset>
The .css: .css:
#sonstiges_on{display:none;}
The Javascript: Javascript:
$(document).ready(function(){
$("#maybe_on").hide();
$("input:radio[name*='Teil']").change(function(){//*= to search for a substring
//='Teil' because the name of the buttons begin with Teil
//for example Teil01_Frage01
if(this.value == 'maybe' && this.checked){
$("#maybe_on").show();
}else{
$("#maybe_on").hide();
}
}); });
As stated in the comments, IDs must be unique, and your maybe radio buttons share the same one.如评论中所述,ID 必须是唯一的,并且您的单选按钮可能共享相同的 ID。 That aside, you could use the following jQuery:除此之外,您可以使用以下 jQuery:
$(document).ready(function () {
$(".maybe_on").hide();
$("input:radio[name^='Teil01_']").change(function () {
$(this).closest('.ui-grid-b.ui-responsive').find('.maybe_on').toggle((this.value == 'maybe' && this.checked));
});
});
Created an inpage Javascript function that checks if the input field in question is clicked.创建了一个页内 Javascript 函数,用于检查是否单击了有问题的输入字段。 And if it is, it shows the 'maybe' input.如果是,它会显示“可能”输入。
function yesnoCheck() {
if (document.getElementById('tUmfrage01_p01_f01_q01_radio03').checked) {
document.getElementById('maybe_on').style.display = 'block';
}
else document.getElementById('maybe_on').style.display = 'none';
}
edit http://jsfiddle.net/sanova/03gkgfah/15/编辑http://jsfiddle.net/sanova/03gkgfah/15/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.