2017-03-17 46 views
0

我有一个复选框,此代码检查默认情况下框,如果不加以控制,留选中

<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> 

我需要有此框默认选中,但也允许它被选中,如果保存留选中。

+0

当您取消选中并保存它时,它会保持检查状态吗? – Highlight

回答

1

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

<input type="checkbox" checked>Checkbox</input>

+0

打我吧! @MHeredia如果你不选中它,我相当肯定它会在保存时被取消选中。 –

+0

正确。我有这个,但它保持检查,即使取消选中和保存。 – MHeredia

+0

@ChrisHappy不幸的是,它并不是因为代码存在于.php模板文件中 – MHeredia

0

使从激活一个jQuery isset PHP

例如:

<script type="text/javascript">function uncheck(){$(#includ_top).attr('checked', false)}</script> 
if(isset($_POST['submit'])) 
{<script>uncheck();</script>} 
0
  1. 不能有2个输入元素同名include_top

  2. 输出您的$_POST["include_top"]$res["include_top"]以查看哪一个使该条件成立。

一个示例代码是这样的:

     <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> 
  • 如果你想让它默认为真,你应该更好地检查反向这样的:

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