【问题标题】:Maintain visibility of a form-textinput when checkbox is checked选中复选框时保持表单文本输入的可见性
【发布时间】:2020-03-31 05:13:13
【问题描述】:

在我的 HTML 中,我有一个表单,用户可以在其中选择“其他”复选框,然后出现一个文本框。否则文本框被隐藏。您可以在下面找到我的代码。 但是如果用户选择“其他”,输入他的文本并提交表单,文本框将再次隐藏 - 尽管复选框保持选中状态(保存在 localStorage 中)。我在这里找不到我的错误。

表格:

<label class="form-check">
       <input class="form-check-input" name="filetype" type="checkbox" id="other" value="" onclick="save()">
       <span class="form-check-label"
             <input placeholder="e.g. 'msg'" name="other" onsubmit="save();" class="form-control input-lg" type="text" id="otherValue" value="{{extension}}">
       </span>
</label> <!-- form-check -->

可见/隐藏

 <!--"Other"-filter-->
      <script type="text/javascript">
        var otherCheckbox = document.querySelector('input[id="other"]');
        var otherText = document.querySelector('input[id="otherValue"]');
        otherText.style.visibility = 'hidden';

        otherCheckbox.onchange = function(){
            if(otherCheckbox.checked) {
                otherText.style.visibility = 'visible';
                otherCheckbox.value = otherText.value;
                save();
            } else {
                otherText.style.visibility = 'hidden';
            }
        };
      </script>

试图通过将信息保存在 sessionStorage 中来解决此问题,但它仍然不起作用。

 <!--Save Checkbox-State-->
      <script type="text/javascript">
        const checkboxen = [...document.querySelectorAll("[type=checkbox]")].map(inp => inp.id); //list of all checkbox-IDs

        function save(){
            for (var i = 0 ; i< checkboxen.length; i++){
                var id = checkboxen[i];
                var checkbox = document.getElementById(id);
                sessionStorage.setItem(id,checkbox.checked);
            }
            var other = document.getElementById('otherValue');
            sessionStorage.setItem('otherValue',other.style.visibility);
        }
        function load(){
            for (var i = 0 ; i< checkboxen.length; i++){
                var id = checkboxen[i];
                var checked =JSON.parse(sessionStorage.getItem(id));
                document.getElementById(id).checked = checked;
            }
            var other = JSON.parse(sessionStorage.getItem('otherValue'));
            document.getElementById('otherValue').style.visibility = other;
        }
        function deleteCheckbox(){         
            sessionStorage.clear();
        }
       </script>

感谢您的帮助

【问题讨论】:

  • 那个函数load在何时何地被调用......?
  • 如果复选框在提交后保持正确选中状态,您可以在页面重新加载时检查其状态并相应地设置文本框可见性。
  • @04FS 在 body-element onload = "load()"
  • @David 如何在 JS 中编写“页面重新加载时”?使用 `var other = document.getElementById('other'); var otherText = document.getElementById('otherValue'); if (other.checked){ otherText.style.visibility = 'visible'; } else { otherText.style.visibility = '隐藏'; }' 没有任何反应...

标签: javascript jquery html forms session-storage


【解决方案1】:

propjquery:

<script>
    $(function(){
       var other = localStorage.input === 'true'? true: false;
       $('input').prop('checked', other);
    });

    $('input').on('change', function() {
       localStorage.input = $(this).is(':checked');
       console.log($(this).is(':checked'));
    });
</script>

【讨论】:

  • 不幸的是,这对我不起作用。现在单击一个复选框时,所有复选框都被选中,但文本字段仍然隐藏...
  • 您可以使用$('selector') -&gt; $('input[name=other]') 选择特殊选择器,然后重试。
  • 我对 jQuery 不是很熟悉。您能告诉我在哪里进行这些更改吗?
【解决方案2】:

这是我的解决方案:

<script type="text/javascript">
        var other = document.getElementById('other');
        var otherText =document.querySelector('input[id="otherValue"]');
        $(document).ready(function(){
            if (other.checked){
                otherText.style.visibility = 'visible';
                otherText.value = "{{extension}}";
                other.value  = "{{extension}}";
            } else {
                otherText.style.visibility = 'hidden';
                 otherText.value = "";
            }
         });

【讨论】:

  • 您能否简要解释一下您解决了什么问题以及如何解决的,以便将来阅读这些问题的人可能会有所收获。
  • 如果复选框在提交后保持选中状态,您可以在页面重新加载时检查其状态并相应地设置文本框可见性,因此请使用$(document).ready(function() thx to @David
  • 我的意思是你应该用额外的信息修改你的答案。你不必向我解释:)
猜你喜欢
  • 2013-12-23
  • 2023-03-11
  • 1970-01-01
  • 2017-12-17
  • 2012-05-07
  • 1970-01-01
  • 2013-08-10
  • 2015-01-22
  • 2017-12-25
相关资源
最近更新 更多