我有一堆默认选中的复选框。我的用户可能会取消选中一些复选框(如果有的话),并选中其余的复选框。

是否有任何方法使表单POST未选中的复选框,而不是选中的复选框?


当前回答

我的做法有点不同。首先,我更改了所有未选中复选框的值。到“0”,然后全部选中,因此该值将被提交。

函数checkboxvalues () { $ (" # checkbox-container输入:复选框"). each函数({ if ($ () .prop(“检查”)! = true) { (美元).val(“0”); (美元)。道具(“检查”,真正的); } }); }

其他回答

我实际上会这样做。

有我的隐藏输入字段与复选框输入相同的名称

<input type="hidden" name="checkbox_name[]" value="0" />
<input type="checkbox" name="checkbox_name[]" value="1" />

然后当我post时,我首先删除在$_POST数组中拾取的重复值,然后显示每个唯一的值。

  $posted = array_unique($_POST['checkbox_name']);
  foreach($posted as $value){
    print $value;
  }

我从一个帖子中得到了这个,从数组中删除重复值

这是对之前答案的一种尝试,以自动保留具有特定值(在本例中为0)的未选中复选框,而不会在提交时选中所有复选框。

$("form").submit(function () {
    let this_master = $(this);

    // Remove any of the hidden values that may already be there (if the user previously canceled the submit)
    this_master.find("*[id^='hiddenchkinput_']").remove();

    // Get all unchecked checkboxes
    this_master.find('input:checkbox:not(:checked)').each(function () {
      let thisChk = $(this);

      // Create a hidden input with the same name as the checkbox
      let newInput = document.createElement('input');
      $(newInput).attr('name', thisChk.attr('id'))
        .attr('id', 'hiddenchkinput_' + thisChk.attr('id'))
        .attr('type', 'hidden')
        .val('0');
      // Append the new input to the end of the form
      this_master.append(newInput);
    });
})
$('input[type=checkbox]').on("change",function(){
    var target = $(this).parent().find('input[type=hidden]').val();
    if(target == 0)
    {
        target = 1;
    }
    else
    {
        target = 0;
    }
    $(this).parent().find('input[type=hidden]').val(target);
});

<p>
    <input type="checkbox" />
    <input type="hidden" name="test_checkbox[]" value="0" />
</p>
<p>
    <input type="checkbox" />
    <input type="hidden" name="test_checkbox[]" value="0" />
</p>
<p>
    <input type="checkbox" />
    <input type="hidden" name="test_checkbox[]" value="0" />
</p>

如果省略复选框的名称,则不会通过。 只有test_checkbox数组。

可能看起来很傻,但对我很管用。主要缺点是在视觉上是一个单选按钮,而不是一个复选框,但它不需要任何javascript。

HTML

Initialy checked
<span><!-- set the check attribute for the one that represents the initial value-->
<input type="radio" name="a" value="1" checked>
<input type="radio" name="a" value="0">
</span>

<br/>
Initialy unchecked
<span><!-- set the check attribute for the one that represents the initial value-->
<input type="radio" name="b" value="1">
<input type="radio" name="b" value="0" checked>
</span>

和CSS

span input
{position: absolute; opacity: 0.99}

span input:checked
{z-index: -10;}

span input[value="0"]
{opacity: 0;}

小提琴在这里

我想听到你在这段代码中发现的任何问题,因为我在生产中使用它

我也喜欢这个解决方案,你只是发布一个额外的输入字段,使用JavaScript似乎有点hack对我来说。

取决于你使用什么你的后端将取决于哪个输入先去。

对于使用第一次出现的服务器后端(JSP),您应该执行以下操作。

  <input type="checkbox" value="1" name="checkbox_1"/>
  <input type="hidden" value="0" name="checkbox_1"/>

对于使用最后一次出现的服务器后端(PHP、Rails),您应该执行以下操作。

  <input type="hidden" value="0" name="checkbox_1"/>
  <input type="checkbox" value="1" name="checkbox_1"/>

对于服务器后端,其中所有事件都存储在列表数据类型([],数组)中。(Python / Zope)

你可以按照你喜欢的任何顺序发布,你只需要尝试从输入中获得带有复选框类型属性的值。如果复选框在隐藏元素之前,列表的第一个索引如果复选框在隐藏元素之后,列表的最后一个索引。

对于一个服务器后端,所有发生的事件都用逗号连接(ASP。Net / iis) 您需要使用逗号作为分隔符来(拆分/分解)字符串,以创建列表数据类型。([])

现在,如果复选框在隐藏元素之前,您可以尝试获取列表的第一个索引,如果复选框在隐藏元素之后,则可以尝试获取列表的最后一个索引。

图片来源