Single-box add options and remove options in a form _jquery

Source: Internet
Author: User

Selection add option and put it in the last item

HTML code:

<form>
<select name= "Location" id= "Location" >
<option value= "Beijing1" >beijing</ option>
<option value= "Shanghai1" >shanghai</option>
<option value= "CHENGDU1" > chengdu</option>
<option value= "CHANGSHA1" >changsha</option>
</select>
< /form>

JS Code:

var select=document.forms[0].elements[' location '];

First method: Add a radio option

var newoption=document.createelement (' option ');
var newtext=document.createtextnode (' Guangzhou ');
Newoption.appendchild (NewText);
Newoption.setattribute (' value ', ' Guangzhou ');
Select.appendchild (newoption);

The second way: is also to add a single option, is not much simpler, haha but incompatible IE8 and the above version

var newsoption=new Option (' nanchang1 ', ' Nanchang ');
Select.appendchild (newsoption);

The Third Way: Add a single check box to see the best solution, this is convenient and compatible

var nnewoption=new Option (' fengcheng1 ', ' Fengcheng ');
Select.add (nnewoption,undefined);

Removal options:

The first method: Select.remove (i);//index starting from 0

The second method: Select.options[i]=null;

The third method: Select.removechild (Select.options[i])

Here are three ways to uncheck the radio of a single marquee

This article relies on jquery, the first of which is implemented using jquery, and the third Way is based on JS and Dom.

<! DOCTYPE html>  

The above is a small part of the form to introduce the form of the optional box to add options and remove options, I hope to help you, if you have any questions please give me a message, small series will promptly reply to everyone. Here also thank you very much for the cloud Habitat Community website support!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.