如果在重新加载页面后选择了其他文本框,则显示隐藏文本框(Show hidden textbox if others is selected even after reloading the page)

编程入门 行业动态 更新时间:2024-10-26 17:23:34
如果在重新加载页面后选择了其他文本框,则显示隐藏文本框(Show hidden textbox if others is selected even after reloading the page)

我有一个javascript代码

使用Javascript

<script type="text/javascript"> $(document).ready(function() { $("label[for='id_other']").hide(); $("#id_other").hide(); $('#id_subsector').change(function() { $("label[for='id_other']").show(); $("#id_other").show(); if ($('#id_subsector').val() == 'Others') { $("label[for='id_other']").css('display', 'block'); $("#id_other").css('display', 'block'); } else { $("label[for='id_other']").css('display', 'none'); $("#id_other").css('display', 'none'); } }); }); </script>

page.html中

<label class="required" for="id_subsector">Sub-sector:</label> <select id="id_subsector" maxlength="50" name="subsector"> <option value="Auto ancillary">Auto ancillary</option> <option value="Retail">Retail</option> <option value="Life Sciences">Life Sciences</option> <option value="Healthcare">Healthcare</option> <option value="Logistics">Logistics</option> <option value="Food &amp; Agriculture">Food &amp; Agriculture</option> <option value="Printing">Printing</option> <option value="Gems &amp; Jewellery">Gems &amp; Jewellery</option> <option value="Light Engineering">Light Engineering</option> <option value="Chemicals &amp; Dyes">Chemicals &amp; Dyes</option> <option value="Motels &amp; Restaurants">Motels &amp; Restaurants</option> <option value="Luxury &amp; Lifestyle">Luxury &amp; Lifestyle</option> <option value="Power">Power</option> <option value="Electrical and Electronic Goods">Electrical and Electronic Goods</option> <option value="Education">Education</option> <option value="Import/Export">Import/Export</option> <option value="IT/ITES">IT/ITES</option> <option value="Others">Others</option> </select> <label class="required" for="id_other">Others:</label> <input id="id_other" maxlength="50" name="other" type="text">

现在这就像我点击其他人一样,文本框将会显示,我可以输入数据。 由于它是必填字段,当我重新加载页面时,它会消失并显示错误。

如果在重新加载后其他人被选中,我如何显示文本框?/

赞赏答案

I have a javascript code

Javascript

<script type="text/javascript"> $(document).ready(function() { $("label[for='id_other']").hide(); $("#id_other").hide(); $('#id_subsector').change(function() { $("label[for='id_other']").show(); $("#id_other").show(); if ($('#id_subsector').val() == 'Others') { $("label[for='id_other']").css('display', 'block'); $("#id_other").css('display', 'block'); } else { $("label[for='id_other']").css('display', 'none'); $("#id_other").css('display', 'none'); } }); }); </script>

page.html

<label class="required" for="id_subsector">Sub-sector:</label> <select id="id_subsector" maxlength="50" name="subsector"> <option value="Auto ancillary">Auto ancillary</option> <option value="Retail">Retail</option> <option value="Life Sciences">Life Sciences</option> <option value="Healthcare">Healthcare</option> <option value="Logistics">Logistics</option> <option value="Food &amp; Agriculture">Food &amp; Agriculture</option> <option value="Printing">Printing</option> <option value="Gems &amp; Jewellery">Gems &amp; Jewellery</option> <option value="Light Engineering">Light Engineering</option> <option value="Chemicals &amp; Dyes">Chemicals &amp; Dyes</option> <option value="Motels &amp; Restaurants">Motels &amp; Restaurants</option> <option value="Luxury &amp; Lifestyle">Luxury &amp; Lifestyle</option> <option value="Power">Power</option> <option value="Electrical and Electronic Goods">Electrical and Electronic Goods</option> <option value="Education">Education</option> <option value="Import/Export">Import/Export</option> <option value="IT/ITES">IT/ITES</option> <option value="Others">Others</option> </select> <label class="required" for="id_other">Others:</label> <input id="id_other" maxlength="50" name="other" type="text">

Right now this works as when I click on others, the textbox will be shown and I can enter the data. As It is required field, when I reload the page it disappears and shows error..

How can I show the textbox if others has been selected even after reload?/

Appreciated the answers

最满意答案

用它。 这对你有帮助。 我正在使用localStorage ,您在加载页面后选择了其他选项显示文本框

$(document).ready(function() { $("label[for='id_other']").hide(); $("#id_other").hide(); $('#id_subsector').change(function(){ $("label[for='id_other']").show(); $("#id_other").show(); localStorage.setItem("value_option",$('#id_subsector').val()); if($('#id_subsector').val() == 'Others') { $("label[for='id_other']").css('display', 'block'); $("#id_other").css('display', 'block'); } else { $("label[for='id_other']").css('display', 'none'); $("#id_other").css('display', 'none'); } }); //load textbox if others var a= localStorage.getItem("value_option"); if(a!=null){ $("#id_subsector").val(a); if(a == 'Others') { $("label[for='id_other']").css('display', 'block'); $("#id_other").css('display', 'block'); } } });

单击演示

Use it. this is helpful for you. i am using localStorage and You selected others option show textbox after loading page

$(document).ready(function() { $("label[for='id_other']").hide(); $("#id_other").hide(); $('#id_subsector').change(function(){ $("label[for='id_other']").show(); $("#id_other").show(); localStorage.setItem("value_option",$('#id_subsector').val()); if($('#id_subsector').val() == 'Others') { $("label[for='id_other']").css('display', 'block'); $("#id_other").css('display', 'block'); } else { $("label[for='id_other']").css('display', 'none'); $("#id_other").css('display', 'none'); } }); //load textbox if others var a= localStorage.getItem("value_option"); if(a!=null){ $("#id_subsector").val(a); if(a == 'Others') { $("label[for='id_other']").css('display', 'block'); $("#id_other").css('display', 'block'); } } });

Click To Demo

更多推荐

option>,<option,value,电脑培训,计算机培训,IT培训"/> <meta name="d

本文发布于:2023-07-24 17:20:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1248848.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:文本框   选择了   加载   页面   Show

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!