简体   繁体   中英

Radio button - edit

I have a create-account form that has a line like this:

<li>
   <label>Question1</label>
   <input type="radio" name="name1" value="yes">Yes
   <input type="radio" name="name1" value="no">No
</li>

After the account is set, the user can edit the account info. In the edit-account form, how could I display the radio buttons with the initially selected value? I have the value and I want the respective button to be checked when the form is opened.

Thank you.

Cheers

You can add checked="checked" to the default answer (see below)

<li>
   <label>Question1</label>
   <input type="radio" name="name1" checked="checked" value="yes">Yes
   <input type="radio" name="name1" value="no">No
</li>

If 2nd required to be checked

<li>
   <label>Question1</label>
   <input type="radio" name="name1" value="yes">Yes
   <input type="radio" name="name1" value="no" checked>No
</li>

if 1st required to be checked.

<li>
   <label>Question1</label>
   <input type="radio" name="name1" value="yes" checked>Yes
   <input type="radio" name="name1" value="no"> No
</li>

If you need the initial value in java script then you need to write the the java script code for that.

<li>
   <label>Question1</label>
   <input type="radio" name="name1" value="yes" checked="checked">Yes
   <input type="radio" name="name1" value="no">No
</li>

//Try this inside the java script

var valuee=document.getElementByName("name1").value;

OR

If you need the value in servlet or server side , when you submit the button , then you use request parameter for getting that value.

//In Servlet
    String valuee=request.getParameter("name1");

Hope it will help you.

You didn't specify your server side language. For example for ruby and erb, when you have a model Account , method question1 will get an answer "yes" or "no":

<li>
   <label>Question1</label>
   <input type="radio" name="name1" value="yes" <%= "checked" if @account.question1 == "yes" %>>Yes
   <input type="radio" name="name1" value="no" <%= "checked" if @account.question1 == "no" %>>No
</li>

When server renders this page, it checks an answer and renders checked for the corresponding elements.

 <input type="radio" name="name1" value="yes" checked="checked">Yes

or

<input type="radio" name="name1" value="no" checked="checked">NO

Use this value of checked="checked" where ever you want to display the default checked mark

use an attribute Selected="selected"

thats all

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM