Operations in the jquery select drop-down box

Source: Internet
Author: User

Copy codeThe Code is as follows:
// ============ Test code ======================
<Head runat = "server">
<Title> drop-down box test </title>
<Script type = "text/javascript" src = "/js/jquery/jquery-1.3.js"> </script>
<Script type = "text/javascript">
$ (Function (){
Var $ city = $ ("# ddlCity ");
// Fill in some data
For (var I = 1; I <= 10; I ++ ){
$ City. append ($ ("<option/>"). attr ("value", I). text ("city to be selected:" + I ));
}
// $ City. width ("100px"); // adjust the width of the drop-down box by yourself under IE6.0
Var t = 6;
// SetTimeout ("$ (\" # ddlCity \"). val ("+ t +"); ", 1); // solve problems in IE and Firefox in IE6.
Try {$ city. val (6);} catch (e) {} // solve problem 2 in IE6. select must have at least one static option. The IE value is incorrect.
// $ City. val (6); // ie6 returns an error. fireFox and IE8.0 are normal.
Alert ($ city. val ());
$ ("# DdlProvince"). val (101); // All are normal
});
</Script>
</Head>
<Body>
<Form id = "form1" runat = "server">
<Select id = "ddlProvince" name = "ddlProvince">
<Option value = "0"> select </option>
<Option value = "1"> Beijing </option>
<Option value = "60"> Chongqing </option>
<Option value = "101"> Guangdong </option>
</Select>
<Select id = "ddlCity" name = "ddlCity">
<Option value = "0"> select </option>
</Select>
<! -- <Option value = "0"> select </option> to clear all options in ddlCity -->
<Asp: Button ID = "butSave" runat = "server" onclick = "butSave_Click" Text = "Button"/>
</Form>
</Body>
// =========== End test code ===================================

Test Description:
A: static select project
Static select items (can be filled by server scripts) can be directly set using $ ("# drop-down box id"). val (selected value.
Use $ ("# drop-down box id"). val (); to get the value correctly.

B: There is a static
$ ("# SelectId"). val ()
Dynamically created (including a static option such as <option value = "0"> select </option> ).
1. setTimeout ("$ (\" # drop-down box id \ "). val (" + value + ")", 1) settings.
However, after setTimeout is used, $ ("# drop-down box id"). val () is used; the value is incorrect. Of course, the context of your code usually contains a value,
There is no need to get through. val (). The disadvantage of setTimeout is asynchronous execution. The context of setTimeout execution is usually not the current function field.

2. Use try {$ ("# drop-down box id"). val () ;}catch (e) {} to block errors,
This can run normally in firefox and IE6.0, but the value of IE6.0 may fail.
C: All dynamic creation
In this case, try cannot be set in IE6.0.

In addition, the select width cannot be automatically adjusted for dynamically created drop-down items in IE6.0. You need to manually adjust the select width.

Related Article

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.