JavaScript traverses the Select Drop-down box to determine whether the value is equal to the specified value

Source: Internet
Author: User
With jquery more, forget the original JS is how to write, but also need to consolidate.
Requirements: JSP echo a select Drop-down box. Select the specified value.

The user clicks Modify the Select to make an existing value echo. There are two ways to solve this problem
One, JS get the user's Swig_dmselect value, in this case, from the action of the JS get all option, and then traverse to judge.
<script type= "Text/javascript" >
<%
String swjg_dmselect = (String) session.getattribute ("Swjg_ Dmselect "); Gets the value
%>
var obj = document.getelementsbytagname ("option")
//traversal option for
(Var i=0;i< obj.length;i++) {
	if (obj[i].value== ' <%=swjg_dmSelect%> ') {
		obj[i].selected=true;  Equality is selected
	}
}
</script>

You can set the Id,id value value for option

<script type= "Text/javascript" >
<%
String swjg_dmselect = (String) session.getattribute ("Swjg_ Dmselect "); Gets
The value%>
document.getElementById ("<%=swjg_dmSelect%>"). Selected=true;
</script>

In the absence of jquery, the following places have Jam 1, how to write Java code 2 in JS, how to traverse array 3, how to select the specified option

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.