默认情况下,如果未选中,则复选框保持未选中状态

时间:2017-03-17 20:55:52

标签: javascript php jquery checkbox

我有一个包含此代码的复选框

<td align="right">Include In Top Sellers?</td>
                          <td>
                            <input type="hidden" name="include_top"
                            value="0"><input type="checkbox"
                            name="include_top"
                            id="include_top"
                            value="1" <?php echo (($_POST["include_top"] == "1") or ($res["include_top"] == "1")) ? "checked='checked'" : ""; ?>
                            autocomplete="off"/>
                          </td>
                        </tr>

我需要默认选中此复选框,但也可以取消选中此复选框,并在保存时保持未选中状态。

3 个答案:

答案 0 :(得分:1)

这是如何从选中的复选框开始

&#13;
&#13;
<input type="checkbox" checked>Checkbox</input>
&#13;
&#13;
&#13;

答案 1 :(得分:0)

从isset PHP激活jquery

例如:

<script type="text/javascript">function uncheck(){$(#includ_top).attr('checked', false)}</script>
if(isset($_POST['submit']))
{<script>uncheck();</script>}

答案 2 :(得分:0)

  1. 您不能拥有2个同名include_top的输入元素。

  2. 输出您的$_POST["include_top"]$res["include_top"],看看哪一个条件为真。

  3. 示例代码如下:

                          <td>
                            <!--input type="hidden" name="include_top"
                            value="0"-->
                           <?php var_dump($_POST["include_top"], $res["include_top"]); ?>
                            <input type="checkbox"
                            name="include_top"
                            id="include_top"
                            value="1" <?php echo (($_POST["include_top"] == "1") or ($res["include_top"] == "1")) ? "checked='checked'" : ""; ?>
                            autocomplete="off"/>
                          </td>
    
    1. 如果您希望默认为true,则应该更好地反复检查:

      <?php echo (($_POST["include_top"] != "1") and ($res["include_top"] != "1")) ? "" : "checked='checked'"; ?>
      
相关问题