简体   繁体   English

如果选中复选框切换,则在切换 div 中需要输入字段

[英]if checkbox toggle checked then make a input field required in the toggle div

I use a script to toggle some div´s with javascript.我使用脚本用 javascript 切换一些 div。 I want to make some input fields "required" in the toggle div if the checkbox is checked to show the toggle div.如果选中复选框以显示切换 div,我想在切换 div 中设置一些“必需”输入字段。

Can someone figure it out ?有人能弄清楚吗? that is work?那是工作吗?

 function show(id) { if(document.getElementById) { var mydiv = document.getElementById(id); mydiv.style.display = (mydiv.style.display=='block'?'none':'block'); } } var $myCheckbox = $('#neu_ma'), $required = $('.required'); $myCheckbox.on('click', function() { this.checked ? $required.prop('required', true) : $required.prop('required', false); });
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <form method="post" name="berechtigungsantrag"> <fieldset> <legend><input type="checkbox" name="neu_ma" id="neu_ma" onclick="javascript:show('neuer_mitarbeiter');"> Neuer Mitarbeiter </legend> <div style="display: none" id="neuer_mitarbeiter"> <label for="eintritt_datum">Eintrittsdatum:</label> <div><input type="date" name="eintritt_datum" id="eintritt_datum" class="required" /></div> <label for="befristung_datum">Befristungsdatum:</label> <div><input type="date" name="befristung_datum" id="befristung_datum"/></div> </div> </fieldset><!-- End of fieldset --> <input class="btn" type="submit" value="Speichern" name=save /> </form>

Answer:回答:

You're trying to listen an element id that does not exist(neu_ma) - you haven't given your checkbox an id.您正在尝试侦听不存在的元素 ID (neu_ma) - 您没有为复选框指定 ID。

<input type="checkbox" name="neu_ma" onclick="javascript:show('neuer_mitarbeiter');"> 

So just give it an ID:所以只要给它一个ID:

<input type="checkbox" name="neu_ma" id="neu_ma" onclick="javascript:show('neuer_mitarbeiter');"> 

This is just an example how you could do it:这只是您如何做到的一个示例:

Add some specific class to inputs that need to be affected by the state of your checkbox.向需要受复选框状态影响的输入添加一些特定类。

I made an example HTML:我做了一个示例 HTML:

<form>
    <input type="checkbox" id="myCheckbox" />
    <input type="text" class="required" />
    <input type="text" />
    <input type="text" class="required" />

    <button type="submit">Submit</button>
</form>

As you can see, some of the inputs have a class called "required".如您所见,一些输入有一个名为“required”的类。

Next I check checkbox status(checked or not).接下来我检查复选框状态(选中与否)。 And depending on the result make elements with "required" class required(or not) using jQuery's prop() method.并根据结果使用 jQuery 的 prop() 方法使具有“必需”类的元素需要(或不需要)。

Working example:工作示例:

 var $myCheckbox = $('#myCheckbox'), $required = $('.required'); $myCheckbox.on('click', function() { this.checked ? $required.prop('required', true) : $required.prop('required', false); });
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <form> <input type="checkbox" id="myCheckbox" /> <input type="text" class="required" /> <input type="text" /> <input type="text" class="required" /> <button type="submit">Submit</button> </form>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM