设置 php 单选按钮默认选中
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8868806/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
set php radio button checked by default
提问by Sufiyan Ghori
I have two radio buttons in my program but when i run it none of them is checked, I want one of them to be checked by default, how can i achieve this ?
我的程序中有两个单选按钮,但是当我运行它时,它们都没有被选中,我希望默认情况下选中其中一个,我该如何实现?
<li><input type="radio" name="r1" value="o" onClick="submit();" <?php echo ($_SESSION['r1'] == "o") ? 'checked="checked"' : ''; ?> />On</li>
<li><input type="radio" name="r1" value="p" onClick="submit();" <?php echo ($_SESSION['r1'] == "p") ? 'checked="checked"' : ''; ?> />Off</li>
I want the 'On' button to be checked when i open the page for the first time
我希望第一次打开页面时选中“打开”按钮
回答by DevelRoot
Here you go:
干得好:
<li><input type="radio" name="r1" value="o" onClick="submit();" <?php echo ($_SESSION['r1'] != "p") ? 'checked="checked"' : ''; ?> />On</li>
<li><input type="radio" name="r1" value="p" onClick="submit();" <?php echo ($_SESSION['r1'] == "p") ? 'checked="checked"' : ''; ?> />Off</li>
回答by Barif
Something like this:
像这样的东西:
<li><input type="radio" name="r1" value="o" onClick="submit();" CHECKED/>On</li>
回答by zaphod1984
What about this:
那这个呢:
<li><input type="radio" name="r1" value="o" onClick="submit();" <?php echo (!$_SESSION['r1'] || $_SESSION['r1'] == "o") ? 'checked="checked"' : ''; ?> />On</li
回答by zuzuleinen
Well, in order for the On button to checked, the $_SESSION['r1']
must be equal to "o". Have you checked the value of $_SESSION['r1']
?
好吧,为了检查 On 按钮,the$_SESSION['r1']
必须等于“o”。你检查过 的值$_SESSION['r1']
吗?