HTML Forms: Radio buttons with text fields

匿名 (未验证) 提交于 2019-12-03 03:06:01

问题:

This seems like a simple problem, but how do I create a basic HTML form that has a series of radio button options, with the last one being a text field to fill in a custom response (i.e. "Other").

What I have now:

    Reason given for stop? <br>     <input type="radio" name="reason" value="Fit Description">Fit Description<br>     <input type="radio" name="reason" value="Suspicious Behavior">Suspicious Behavior<br>     <input type="radio" name="reason" value="No Reason Given">No Reason Given<br>     <input type="radio" name="reason" value="">Other<br> 

回答1:

Just add a text input field to it.

jsFiddle example



回答2:

  1. Create a text field, and set it to display:none;
  2. Then with jQuery, detect when the 'Other' radio button is checked and show the textbox.
  3. Then on your process script, do and if statement to see if the value of your radio button group is "" (nothing), and grab the post data of the textbox and do what you want with it.


回答3:

Maybe this is a bit outdated, but here is my find on how you can create radio buttons with clickable text beside them in a separate HTML tag. :D

<input type="radio" name="lp-fb-smoker" id="lp-fb-smoker" value="Smoker"><label for="lp-fb-smoker">Smoker</label> <input type="radio" name="lp-fb-smoker" id="lp-fb-non-smoker" value="Non Smoker"><label for="lp-fb-non-smoker"> Non Smoker</label> 

Hope this helps someone out there. :D



回答4:

Simply add a text field as you would normally but give it the same name attribute as the others, that way when accessing them you'll get one of them.

In JavaScript (which I assume you'll be using?) just access these elements and check the the textfield is empty, if it is get the radio buttons.



回答5:

There is a neat way to add text field alongside radio buttons without using functions or Javascript (jQuery). Just add the radio btn (Other) and the text field next to it, on the top of your HTML form. Here is what i use:

Color:

<input type="radio" name="title[<?=$red?>][color]" value="" <?php if ($row['color'] != ' ') {echo 'checked';} ?> />Other <input type="text" name="title[<?=$red?>][color]" value="<?php echo $row['color'] ?>" style="width: 200px;" /> |  <input type="radio" name="title[<?=$red?>][color]" value="natural" <?php if ($row['color'] == 'natural') {echo 'checked';} ?> />natural|  <input type="radio" name="title[<?=$red?>][color]" value="stain" <?php if ($row['color'] == 'stain') {echo 'checked';} ?> />stain |  <input type="radio" name="title[<?=$red?>][color]" value="white" <?php if ($row['color'] == 'white') {echo 'checked';} ?> />white| 

Basically you do not put value on the "Other" radio input, but on the text input so whatever you write in the text field will be send to db - its 1st field in the FORM. If nothing in the text field - the other checked radio input will be processed.

Hope this helps.



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!