Select option box created by Ajax in jquery

Source: Internet
Author: User

Using jquery ajax to interact with PHP, the select option box is dynamically created for the XML data source.

Not to mention, serving:

 

Javascript:

/** <Br/> * aajax creates select <br/> * @ return void <br/> */<br/> function ajax_category () {<br/> var OBJ ={}; <br/> obj. mode = "city"; <br/> $. ajax ({<br/> type: "Post" <br/>, URL: "ajax_category.php" <br/> // the data source is XML <br/>, datetype: "XML" <br/>, data: $. param (OBJ) <br/>, success: function (XML) {<br/> // Delete the original select <br/> $ ("select [name = category]"). remove (); </P> <p>/* XML as the data source <br/> <D ATA> <br/> <Category> <br/> <ID> 1 </ID> <br/> <Name> Beijing </Name> <br/> </category> <br/> <Category> <br/> <ID> 2 </ID> <br/> <Name> Shanghai </Name> <br/> </Category> <br/> <Category> <br/> <ID> 1 </ID> <br/> <Name> Shenzhen </Name> <br/> </Category> <br/> </data> <br/> */<br/> if ($ ("category ", XML ). text ()! = "") {<Br/> // create select <br/> var Category = $ ("<SELECT>"); <br/> $ (category ). ATTR ("name", "category"); <br/> category (category).css ({"display": "Block", "width": "120px", "margin-top ": "3px"}); <br/> // new options (Label, value) <br/> $ (Category) [0]. options. add (New Option ("select", ""); <br/> $ ("category", XML ). each (function () {<br/> var id = $ ("ID", this ). text (); <br/> var name = $ ("name", this ). text (); </P> <p> // create options <br/> $ (Category) [0]. options. add (New Option (name, ID); <br/> }); // each end <br/> // insert internally <br/> $ ("body "). append (category); <br/>}// if end <br/>}// success end <br/> }); // Ajax </P> <p>}



PHP:

<? Php <br/>/** <br/> * Ajax obtains the XML data source <br/> */<br/> var $ xml = '<? XML version = "1.0" encoding = "UTF-8"?> '; <Br/> $ items = ''; <br/> switch ($ _ post ['Mode'];) {<br/> case 'city ': <br/> $ items = '<Category> <ID> 1 </ID> <Name> Beijing </Name> </Category>' <br/>. '<Category> <ID> 2 </ID> <Name> Shanghai </Name> </Category>'; <br/> break; <br/> default: break; <br/>}< br/> $ XML. = "<data >{$ items} </data>"; <br/> header ('content-type: text/XML; charset = UTF-8 '); <br/> echo mb_convert_encoding ($ XML, 'utf-8', 'auto'); <br/> exit;
 

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.