How to obtain the values of all Select Options

Source: Internet
Author: User

Q:
There is a check select in a page because option changes dynamically.
I want to obtain the values of all options in the SELECT statement after submission.
<Select multiple name = test>
<Option value = 1> 1 </option>
<Option value = 2> 2 </option>
</SELECT>
Whether or not selected
Can someone tell me how to do this?
______________________________________________________________________________________________
Answer 1:
VaR o = Document. formname. Test
S ="
For (I = 0; I <O. Options. length; I ++ ){
S + = O. Options [I]. Text + ":"
If (O. Options [I]. Selected) Alert (O. Options [I]. Text)
}
Alert (s)
______________________________________________________________________________________________
Answer 2:
It is best not to use test as the name, it will be wrong
______________________________________________________________________________________________
Answer 3:
Obtain all values after submission.
All options must be selected before submission.

Listlength = FRM. selectoperpoint. Options. length;
For (I = 0; I <listlength; I ++)
{
FRM. selectoperpoint. Options [I]. Selected = true;
}
______________________________________________________________________________________________
Answer 4:
This statement can only be used on the page where test is located. I want to get the result on the submitted page.
______________________________________________________________________________________________
Answer 5:
What I want to do is an employment system, which has an employment location selection. This is a multiple-choice, and another multiple-choice select
Click another select Check item to remove the selected option.
I want to get the value of all options after submitting the ticket, because one option represents a place where the user is willing to work.
______________________________________________________________________________________________
Answer 6:
I think this is what the Internet employment system does, but I don't know how to process the submitted data and store the user's selected location in the database, I think he should have obtained the value of all options, and I tried it. He can get all the values regardless of whether you select the option or not.
______________________________________________________________________________________________
Answer 7:
<HTML>
<Script language = "JavaScript">
Function btnclick ()
{
VaR I
VaR S = ""
For (I = 0; I <document. All. Item ("num"). length; I ++)
{
S = S + document. All. Item ("num"). Options [I]. Value + ","
Alert (s)
}

Document. All. Item ("result"). value = s
Alert (
"S =" + document. All. Item ("result"). value)
Document. All. Item ("fmsel"). Submit
}
</SCRIPT>
<Head>
<Meta http-equiv = "Content-Type" content = "text/html; charset = gb2312">
<Meta name = "generator" content = "Microsoft FrontPage 4.0">
<Meta name = "progid" content = "FrontPage. Editor. Document">
<Title> New Page 1 </title>
</Head>

<Body>
<Form name = fmsel action "">
<Select name = num size = 1> <option value = 1> 1 </option> <option value = 2> 2 </option> </SELECT>
<Input type = hidden name = result value = "">
<Input type = button name = BTN value = click onclick = "btnclick ();">
</Form>
</Body>

</Html>

______________________________________________________________________________________________
Answer 8:
Options [I]. Text
______________________________________________________________________________________________
Answer 9:
Option operation example
Htm
<HTML>
<Head>
<Style type = "text/CSS">
Table {
Width: 100%;
}
TD {
Text-align: center;
}
Select {
Behavior: URL ("list. HTC"); width: 100px; Height: 200px;
}
Input {
Behavior: URL ("list. HTC ")
}
</Style>
</Head>
<Body>
<Table>
<Tr>
<TD colspan = "3"> Delete moving items, check duplicate items, and sort by value (from small to large) </TD>
</Tr>
</Tr>
<TD>
<Select id = "AAA" multiple ref = "BBB" Order = "value" deleted check>
<Option value = "1"> 1 </option>
<Option value = "2"> 2 </option>
<Option value = "3"> 3 </option>
<Option value = "4"> 4 </option>
<Option value = "5"> 5 </option>
<Option value = "6"> 6 </option>
</SELECT>
</TD>
<TD>
<Input type = "button" value = "select all" ref = "AAA" selectall> <br>
<Input type = "button" value = ">" ref = "AAA"> <br>
<Input type = "button" value = "<" ref = "BBB"> <br>
<Input type = "button" value = "cancel all" ref = "BBB" selectall> <br>
</TD>
<TD>
<Select id = "BBB" multiple ref = "AAA" Order = "value" deleted check>
<Option value = "7"> 7 </option>
</SELECT>
</TD>
</Tr>
</Table>
<HR>
<Table>
<Tr>
<TD colspan = "3"> do not delete moving items, do not check duplicate items, and follow the Ord descending order (from large to small) </TD>
</Tr>
</Tr>
<TD>
<Select id = "CCC" multiple ref = "DDD" Order = "ord" DESC>
<Option value = "1" ord = "1"> 1 </option>
<Option value = "2" ord = "5"> 2 </option>
<Option value = "3" ord = "2"> 3 </option>
<Option value = "4" ord = "6"> 4 </option>
<Option value = "5" ord = "3"> 5 </option>
<Option value = "6" ord = "7"> 6 </option>
</SELECT>
</TD>
<TD>
<Input type = "button" value = "select all" ref = "CCC" selectall> <br>
<Input type = "button" value = ">" ref = "CCC"> <br>
<Input type = "button" value = "<" ref = "DDD"> <br>
<Input type = "button" value = "cancel all" ref = "DDD" selectall> <br>
</TD>
<TD>
<Select id = "DDD" multiple ref = "CCC" Order = "ord" DESC>
<Option value = "7" ord = "4"> 7 </option>
</SELECT>
</TD>
</Tr>
</Table>
<HR>
<Table>
<Tr>
<TD colspan = "3"> do not delete moving items, check duplicate items, and reverse order by ord (from large to small) </TD>
</Tr>
</Tr>
<TD>
<Select id = "eee" multiple ref = "fff" Order = "ord" DESC check>
<Option value = "1" ord = "1"> 1 </option>
<Option value = "2" ord = "5"> 2 </option>
<Option value = "3" ord = "2"> 3 </option>
<Option value = "4" ord = "6"> 4 </option>
<Option value = "5" ord = "3"> 5 </option>
<Option value = "6" ord = "7"> 6 </option>
</SELECT>
</TD>
<TD>
<Input type = "button" value = "select all" ref = "eee" selectall> <br>
<Input type = "button" value = ">" ref = "eee"> <br>
<Input type = "button" value = "<" ref = "fff"> <br>
<Input type = "button" value = "cancel all" ref = "fff" selectall> <br>
<Input type = "button" value = "select all items in the list on the left" onclick = "Eee. selectall ()"> <br>
<Input type = "button" value = "cancel the selected item" onclick = "Eee. selectnone ()"> <br>
<Input type = "button" value = "delete an item from the list on the left" onclick = "Eee. Deleted ()"> <br>
</TD>
<TD>
<Select id = "fff" multiple ref = "eee" Order = "ord" DESC check>
<Option value = "7" ord = "4"> 7 </option>
</SELECT>
</TD>
</Tr>
</Table>
</Body>
</Html>

______________________________________________________________________________________________
Answer 10:
List. HTC

// Delete all selected items
<Public: method name = "deleted"/>
// Select all items
<Public: method name = "selectall"/>
// Cancel the selected item
<Public: method name = "selectnone"/>
<Public: attach event = "onclick" onevent = "doclick ()"/>
<Public: attach event = "ondblclick" onevent = "dodblclick ()"/>
<Script language = "jscript">
// Delete all selected items from the list box. If obj is null, the list box is itself.
Function deleted (OBJ ){
If (OBJ = NULL) OBJ = element;
If (obj. tagname! = "Select") return;
For (VAR I = obj. Options. Length-1; I> = 0; I --){
If (obj. Options [I]. Selected ){
OBJ. Options. Remove (I );
}
}
}
// Select all items in the given list box. If obj is null, the list box is itself.
Function selectall (OBJ ){
If (OBJ = NULL) OBJ = element;
If (obj. tagname! = "Select") return;
VaR length = obj. Options. length;
For (VAR I = 0; I <length; I ++ ){
OBJ. Options [I]. Selected = true;
}
}
// Cancel all selected items in the given list box. If obj is null, the list box is itself.
Function selectnone (OBJ ){
If (OBJ = NULL) OBJ = element;
If (obj. tagname! = "Select") return;
VaR length = obj. Options. length;
For (VAR I = 0; I <length; I ++ ){
OBJ. Options [I]. Selected = false;
}
}
// Check whether the option opt exists in the OBJ list box
Function optionexists (OBJ, OPT ){
VaR length = obj. Options. length;
For (VAR I = 0; I <length; I ++ ){
If (obj. options [I]. TEXT = OPT. text & obj. options [I]. value = OPT. value) // if the value and text of an option are the same as the given option, it indicates that the option exists.
Return true;
}
Return false; // if not, false is returned.
}
// Add the items selected in the obj1 list box to the obj2 list box. Check = true indicates that repeated items are not added to obj2.
Function add (obj1, obj2, check ){
For (VAR I = 0; I <obj1.options. length; I ++ ){
If (obj1.options [I]. Selected) {// if the option is selected, add it to obj2.
If (check) {// repeated check is required
If (optionexists (obj2, obj1.options [I]) {// if the specified option exists in the obj2 list box, perform the next loop
Continue;
}
}
VaR opt = obj1.options [I]. clonenode (false );
Opt. Text = obj1.options [I]. text;
Obj2.options. Add (OPT); // Add the option to ojb2.
}
}
}
// Sort. obj is the object in the list box, order is the sorting attribute, and desc is reverse (true-reverse). Bubble is used. Algorithm Improved Algorithm
Function orderlist (OBJ, order, DESC ){
VaR chg = NULL;
VaR flag = true; // indicates whether the switch is performed.
VaR M = NULL;
VaR n = NULL;
VaR Len = obj. Options. Length-1;
For (var j = Len; j> 0; j --){
Flag = true;
For (var k = 0; k <j; k ++ ){
Eval ("m = parsefloat (obj. Options [K]." + order + ");");
Eval ("n = parsefloat (obj. Options [k + 1]." + order + ");");
If (DESC) {// if it is in inverted order, m and n are exchanged.
VaR TT = m;
M = N;
N = tt;
}
If (M> N) {// If M> N exchanges two options
Chg = obj. Options [K]. clonenode (false );
Chg. Text = obj. Options [K]. text;
OBJ. Options [k] = obj. Options [k + 1]. clonenode (false );
OBJ. Options [K]. Text = obj. Options [k + 1]. text;
OBJ. Options [k + 1] = chg. clonenode (false );
OBJ. Options [k + 1]. Text = chg. text;
Flag = false; // switched, set to false
}
}
If (FLAG) break; // exit if none are returned at a time
}
}
// Event processing
Function doevent (){
VaR obj1 = NULL; // list box 1 variable
If (window. event. srcelement. tagname = "select") {// If the trigger element is select
Obj1 = Window. event. srcelement; // obj is the select
}
Else if (window. event. srcelement. tagname = "option") {// If the trigger element is option
Obj1 = Window. event. srcelement. parentelement; // obj is the parent element of the trigger element select
}
Else {
If (typeof (window. event. srcelement. Ref )! = "Undefined") {// If ref has been defined
Obj1 = plain Doc ument. getelementbyid (window. event. srcelement. Ref); // obj is the defined element.
If (obj1 = NULL) return;
}
Else
Return;
}
If (obj1 = NULL) return; // If obj is empty, return directly.
If (obj1.tagname! = "Select") return; // If the returned obj is not a list box, it is returned directly.
If (typeof (obj1.ref) = "undefined") return; // if no target list box is specified, return directly.
VaR obj2 = invalid argument Doc ument. getelementbyid (obj1.ref );
If (obj2 = NULL) return; // the target list box is not found and is returned directly.
If (obj2.tagname! = "Select") return; // if the specified object is not a list box, return.
If (typeof (window. event. srcelement. selectall )! = "Undefined") {// if all options are defined
Selectall (obj1 );
}
VaR check = (typeof (obj2.check )! = "Undefined ")? True: false; // determines whether duplicate check items are defined.
Add (obj1, obj2, check); // Add the items selected in obj1 to ojb2.
If (typeof (obj1.deleted )! = "Undefined") {// Delete the original item if it is defined
Deleted (obj1); // Delete the original item
}
If (typeof (obj2.order )! = "Undefined") {// specify the order
VaR order = obj2.order;
VaR DESC = (typeof (obj2.desc) = "undefined ")? False: true; // determines whether reverse order is defined.
Orderlist (obj2, order, DESC); // sort
}
}
// Process the Click Event
Function doclick (){
If (window. event. srcelement. tagname = "select" | window. event. srcelement. tagname = "option") return;
Doevent ();
}
// Process double-click events
Function dodblclick (){
Doevent ();
}
</SCRIPT>

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.