【问题标题】:Make html checkbox checked and non editable使 html 复选框被选中且不可编辑
【发布时间】:2013-03-06 05:19:12
【问题描述】:

我有一个 HTML 表单,带有几个输入复选框。我需要其中两个预先选择且不可编辑。我已经尝试过“检查”和“禁用”,但禁用的输入不包括在表单提交中。还有其他方法吗?我希望复选框被选中且不可编辑,并且它的值在表单提交时提交。

【问题讨论】:

    标签: html


    【解决方案1】:

    如果选中并禁用为您提供所需的外观,则添加一些隐藏表单字段以提交您需要的数据。

    【讨论】:

    • 我正要这么说
    • 我正在输入同样的答案。 xD
    【解决方案2】:

    我有一个解决您的问题的方法。

    您可以将选中的复选框值存储在隐藏字段中,并在表单提交时访问隐藏字段。

    在 html 方面:

    选中并禁用复选框。同时将隐藏字段中选中的复选框值保存为逗号分隔的字符串。

    在服务端: 从逗号分隔的字符串中获取值作为字符串数组并将数组的值保存在数据库中。

    【讨论】:

      【解决方案3】:

      如果您更喜欢使用脚本,请尝试以下方式。你没有在你的问题中提到你是否能够使用它。我希望这至少能对你有所帮助.. :)

      <!DOCTYPE html>
      <html lang="en">
          <head>
              <meta charset="utf-8" />
              <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
              <script>
                  $(document).ready(function(){
                      $('.required-checkbox').each(function(index, value){
                          $(value).change(function(){
                              $(this).prop('checked', true);
                          });
                      });
                  });
              </script>
          </head>
          <body>
              <input type="checkbox" class="required-checkbox" checked />
              <input type="checkbox" />
              <input type="checkbox" class="required-checkbox" checked />
              <input type="checkbox" />
              <input type="checkbox" />
              <input type="checkbox"  />
          </body>
      </html>
      

      【讨论】:

        【解决方案4】:

        您可以在表单提交事件期间移除这些输入的“禁用”状态。

        $('form').submit(function(e) {
            var disabledInputs = $(this).find(':input:disabled');
            disabledInputs.removeAttr('disabled');
            return true;
         });
        

        【讨论】:

          【解决方案5】:

          我用过这个

          <style>
          .read_only {
              pointer-events: none;
          
          }
           </style>
          

          并将此类应用于任何输入表单字段甚至复选框,它可以正常工作,并且复选框的颜色未禁用

          【讨论】:

            【解决方案6】:
            $(":checked").on('click',function(){   //select those element which you want to make preselect and non editable...
             $(this).attr("checked","checked");
            });
            

            你不需要禁用它...

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 2015-03-17
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多