JavaScript dynamically changes Select values

Source: Internet
Author: User

<!doctype HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "http://www.w3.org/tr/xhtml1/dtd/ Xhtml1-transitional.dtd ">
<meta http-equiv= "Content-type" content= "text/html; charset=gb2312 "/>
<title> Web Effects Dynamic Change Select value </title>
<script language= "JavaScript" >
var getselectvalue = funtion (select)
{
var idx = Select.selectedindex,
Option
Value
if (idx >-1) {
option = Select.options[idx];
value = Option.attributes.value;
Return (value && value.specified)? Option.value:option.text);
}
return null;
}
</script>

<body>
<script type= "Text/javascript" >
var getselectvalue = function (select) {
var idx = Sele Ct.selectedindex,
option,
value;
if (idx >-1) {
option = Select.options[idx];
value = Option.attributes.value;
Return (value && value.specified)? Option.value:option.text;
}
return null;
}
</script>
<select id= 111cnnet "onchange=" alert (getselectvalue document.getElementById (' 111cnnet ')) >
<option value= "http://www.111cn.net" > Web page Making Tutorials </option>
<option value= "http ://mb.111cn.net > Templates </option>
</select>
</body>

Other details about the Web page special Select,
1. Determine if there is a value= "Paravalue" Item in the Select option
function Web Effects Selectisexititem (objselect, Objitemvalue) {
var isexit = false;
for (var i = 0; i < objselect.options.length; i++) {
if (Objselect.options[i].value = = Objitemvalue) {
Isexit = true;
Break
}
}
return isexit;
}

2. Add an item to the Select option
function Jsadditemtoselect (Objselect, Objitemtext, Objitemvalue) {
To determine whether there is
if (Jsselectisexititem (Objselect, Objitemvalue)) {
Alert ("The value of the item already exists");
} else {
var varitem = new Option (Objitemtext, Objitemvalue);
Objselect.options.add (Varitem);
Alert ("Join successfully");
}
}

3. Remove an item from the Select option
function Jsremoveitemfromselect (Objselect, Objitemvalue) {
To determine whether there is
if (Jsselectisexititem (Objselect, Objitemvalue)) {
for (var i = 0; i < objselect.options.length; i++) {
if (Objselect.options[i].value = = Objitemvalue) {
Objselect.options.remove (i);
Break
}
}
Alert ("Successful deletion");
} else {
Alert ("The item does not exist in the select");
}
}


4. Delete selected items in select
function Jsremoveselecteditemfromselect (objselect) {
var length = objselect.options.length-1;
for (var i = length; I >= 0; i--) {
if (objselect[i].selected = = True) {
Objselect.options[i] = null;
}
}
}

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.