初始化和记住单选按钮(Initializing and remembering radio buttons)

编程入门 行业动态 更新时间:2024-10-28 10:29:42
初始化和记住单选按钮(Initializing and remembering radio buttons)

我在这里和其他地方读过很多关于记住单选按钮的帖子。 我还阅读了很多关于初始化一个单选按钮的帖子。 我没有读过很多东西就是将两者结合起来。 如果可能的话,我想在输入语句中这样做。

记住的例子(即,在重新显示页面时保留突出显示的单选按钮):

<input type="radio" name="sex" value="Male" <?php echo ($sex=='Male')?'checked':'' ? >size="17">Male <input type="radio" name="sex" value="Female" <?php echo ($sex=='Female')?'checked':'' ?> size="17">Female

要么

<input type="radio" name="veg" value="cabbage" <?php if(!isset($veg)){print " checked=\"checked\"";} if(isset($veg) && $veg == "cabbage"){print " checked=\"checked\"";} ?>> cabbage <input type="radio" name="veg" value="onion" <?php if(isset($veg) && $veg == "onion"){print " checked=\"checked\"";} ?>> onion

要么

<input name="searchtype" type="radio" value="artist" <? if ($searchtype == 'artist') echo 'checked'; ?>> &nbsp;&nbsp;&nbsp; Title: <input name="searchtype" type="radio" value="title" <? if ($searchtype == 'title') echo 'checked'; ?>> &nbsp;&nbsp;&nbsp; Year: <input name="searchtype" type="radio" value="year" <? if ($searchtype == 'year') echo 'checked'; ?>>

第一个似乎最简单,但我不能让它工作。 我不断收到一些错误,可能是语法错误。 这是我的部分代码:

<input type="radio" name="typ" value="exact" <?php if ($_GET["typ"]=="exact") echo "checked=\"checked\""; ?>Exact <input type="radio" name="typ" value="starts" <?php if ($_GET["typ"]=="starts") echo "checked=\"checked\""; ?>Starts with <input type="radio" name="typ" value="sounds" <?php if ($_GET["typ"]=="sounds") echo "checked=\"checked\""; ?>Soundex</td></tr> <input type="submit" value="Find" /></td></tr>

第二个问题是以某种方式将默认值合并到上面。

我的尝试涉及使用上面代码的第三种格式并尝试最初测试null。 像这样的东西。 再次它不起作用:

完美从Soundex开始

以下代码有效,但是很详细:

$exact_status = 'unchecked'; $starts_status = 'unchecked'; $sounds_status = 'unchecked'; if (isset($_POST['typ'])) { $selected_radio = $_POST['typ']; if ($selected_radio == 'exact') {$exact_status = 'checked';} else if ($selected_radio == 'starts') {$starts_status = 'checked';} else if ($selected_radio == 'sounds') {$sounds_status = 'checked';} } else { $exact_status = 'checked';} echo '<p>Enter Surname and optionally Given Name(s): <form action="'.$_SERVER['PHP_SELF'].'" method="post"> <table cellpadding="4" cellspacing="0"> <tr><td valign="top">Surname:</td><td><input type="text" size=40 name="surname" value="'.$surname.'" /></td><td>Required</td></tr> <tr><td valign="top">Given Name(s):</td><td><input type="text" size=40 name="firstname" value="'.$firstname.'" /></td><td>Optional</td></tr> <tr><td valign="top">Search Type:</td><td> <input type="radio" name="typ" value="exact"'.$exact_status.'>Exact <input type="radio" name="typ" value="starts"'.$starts_status.'>Starts with <input type="radio" name="typ" value="sounds"'.$sounds_status.'>Soundex </td></tr> <tr><td></td><td><input type="submit" value="Find" /></td></tr> </table> </form> <p>';

根据下面的评论我尝试了这个。 它几乎可以工作。 第一个输入框有效。 接下来的两个只输出单词“checked”而不是输入语句???

if (!isset($_POST['typ'])) {$radio1 = '<input type="radio" name="typ" value="exact" checked>Exact';} else {$radio1 = '<input type="radio" name="typ" value="exact"'.($_GET['typ'] =="exact")?"checked":''.'>Exact';} $radio2 = '<input type="radio" name="typ" value="starts"'.($_GET['typ'] =="starts")?"checked":''.'>Starts with'; $radio3 = '<input type="radio" name="typ" value="sounds"'.($_GET['typ'] =="sounds")?"checked":''.'>Soundex'; echo '<p>Enter Surname and optionally Given Name(s): <form action="'.$_SERVER['PHP_SELF'].'" method="post"> <table cellpadding="4" cellspacing="0"> <tr><td valign="top">Surname:</td><td><input type="text" size=40 name="surname" value="'.$surname.'" /></td><td>Required</td></tr> <tr><td valign="top">Given Name(s):</td><td><input type="text" size=40 name="firstname" value="'.$firstname.'" /></td><td>Optional</td></tr> <tr><td valign="top">Search Type:</td><td>',$radio1,$radio2,$radio3, '</td></tr> <tr><td></td><td><input type="submit" value="Find" /></td></tr> </table> </form> <p>';

换句话说,在第一个单选按钮后,它说:Exactcheckedchecked。 缺少接下来的两个按钮。

I have read a lot of posts here and elsewhere about remembering radio buttons. I have also read a lot of posts about initialising one radio button. What I have not read a lot of is combining the two. I am wanting to do it within the input statement if possible.

Examples of remembering (ie. retaining the highlighted radio button on re-display of the page):

<input type="radio" name="sex" value="Male" <?php echo ($sex=='Male')?'checked':'' ? >size="17">Male <input type="radio" name="sex" value="Female" <?php echo ($sex=='Female')?'checked':'' ?> size="17">Female

or

<input type="radio" name="veg" value="cabbage" <?php if(!isset($veg)){print " checked=\"checked\"";} if(isset($veg) && $veg == "cabbage"){print " checked=\"checked\"";} ?>> cabbage <input type="radio" name="veg" value="onion" <?php if(isset($veg) && $veg == "onion"){print " checked=\"checked\"";} ?>> onion

or

<input name="searchtype" type="radio" value="artist" <? if ($searchtype == 'artist') echo 'checked'; ?>> &nbsp;&nbsp;&nbsp; Title: <input name="searchtype" type="radio" value="title" <? if ($searchtype == 'title') echo 'checked'; ?>> &nbsp;&nbsp;&nbsp; Year: <input name="searchtype" type="radio" value="year" <? if ($searchtype == 'year') echo 'checked'; ?>>

The first one seems simplest, but I cannot get it to work. I keep getting some error, presumably syntax. Here is my partial code:

<input type="radio" name="typ" value="exact" <?php if ($_GET["typ"]=="exact") echo "checked=\"checked\""; ?>Exact <input type="radio" name="typ" value="starts" <?php if ($_GET["typ"]=="starts") echo "checked=\"checked\""; ?>Starts with <input type="radio" name="typ" value="sounds" <?php if ($_GET["typ"]=="sounds") echo "checked=\"checked\""; ?>Soundex</td></tr> <input type="submit" value="Find" /></td></tr>

The second issue is incorporating the default value into the above somehow.

My attempt involved using the third format of the code above and trying to test for null initially. Something like this. Once again it did not work:

Exact Starts with Soundex

The following code works, but is verbose:

$exact_status = 'unchecked'; $starts_status = 'unchecked'; $sounds_status = 'unchecked'; if (isset($_POST['typ'])) { $selected_radio = $_POST['typ']; if ($selected_radio == 'exact') {$exact_status = 'checked';} else if ($selected_radio == 'starts') {$starts_status = 'checked';} else if ($selected_radio == 'sounds') {$sounds_status = 'checked';} } else { $exact_status = 'checked';} echo '<p>Enter Surname and optionally Given Name(s): <form action="'.$_SERVER['PHP_SELF'].'" method="post"> <table cellpadding="4" cellspacing="0"> <tr><td valign="top">Surname:</td><td><input type="text" size=40 name="surname" value="'.$surname.'" /></td><td>Required</td></tr> <tr><td valign="top">Given Name(s):</td><td><input type="text" size=40 name="firstname" value="'.$firstname.'" /></td><td>Optional</td></tr> <tr><td valign="top">Search Type:</td><td> <input type="radio" name="typ" value="exact"'.$exact_status.'>Exact <input type="radio" name="typ" value="starts"'.$starts_status.'>Starts with <input type="radio" name="typ" value="sounds"'.$sounds_status.'>Soundex </td></tr> <tr><td></td><td><input type="submit" value="Find" /></td></tr> </table> </form> <p>';

Based on comments below I tried this. It almost works. The first input box works. The next two just output the word "checked" instead of the input statement???

if (!isset($_POST['typ'])) {$radio1 = '<input type="radio" name="typ" value="exact" checked>Exact';} else {$radio1 = '<input type="radio" name="typ" value="exact"'.($_GET['typ'] =="exact")?"checked":''.'>Exact';} $radio2 = '<input type="radio" name="typ" value="starts"'.($_GET['typ'] =="starts")?"checked":''.'>Starts with'; $radio3 = '<input type="radio" name="typ" value="sounds"'.($_GET['typ'] =="sounds")?"checked":''.'>Soundex'; echo '<p>Enter Surname and optionally Given Name(s): <form action="'.$_SERVER['PHP_SELF'].'" method="post"> <table cellpadding="4" cellspacing="0"> <tr><td valign="top">Surname:</td><td><input type="text" size=40 name="surname" value="'.$surname.'" /></td><td>Required</td></tr> <tr><td valign="top">Given Name(s):</td><td><input type="text" size=40 name="firstname" value="'.$firstname.'" /></td><td>Optional</td></tr> <tr><td valign="top">Search Type:</td><td>',$radio1,$radio2,$radio3, '</td></tr> <tr><td></td><td><input type="submit" value="Find" /></td></tr> </table> </form> <p>';

In other words after the first radio button it says: Exactcheckedchecked. The next two buttons are missing.

最满意答案

<input type="radio" name="typ" value="exact" <?php echo ($_GET['typ'] =="exact")?"checked":'' ?>>Exact <input type="radio" name="typ" value="exact" <?php echo ($_GET['typ'] =="starts")?"checked":'' ?>>Starts with <input type="radio" name="typ" value="exact" <?php echo ($_GET['typ'] =="sounds")?"checked":'' ?>>Soundex <input type="submit" value="Find" /></td></tr>

像上面这样的东西应该可以解决问题。 它被称为速记,如果并且 - 我会说 - 仅在类似上面的情况下有用。

请阅读更多相关信息: http : //davidwalsh.name/php-ternary-examples其中有很多关于如何使用它的示例。

Lycka直到!

<input type="radio" name="typ" value="exact" <?php echo ($_GET['typ'] =="exact")?"checked":'' ?>>Exact <input type="radio" name="typ" value="exact" <?php echo ($_GET['typ'] =="starts")?"checked":'' ?>>Starts with <input type="radio" name="typ" value="exact" <?php echo ($_GET['typ'] =="sounds")?"checked":'' ?>>Soundex <input type="submit" value="Find" /></td></tr>

Something like above should do the trick. It is called a shorthand if and is - I would say - only useful in above-like cases.

Please read more about it on:http://davidwalsh.name/php-ternary-examples Which has lots of examples on how to use it.

Lycka till!

更多推荐

本文发布于:2023-07-17 10:13:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1142846.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:初始化   单选   按钮   Initializing   radio

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!