Ajax+json cascading menu Implementation code _json

Source: Internet
Author: User
First Drop-down Box:
Copy Code code as follows:

<select id= "Select" Name= "Rawfoodbasic.category" onchange= "Dochange (this.value);" >
The value in the first Drop-down box
</select>

The second Drop-down box:
Copy Code code as follows:

<select id= "Subselect" name= "Rawfoodbasic.absorb" onchange= "Javascript:alert" (This.value); ">/ To 3 and the menu can also be written at the appropriate time
<option value= "0" >--Please select level two menu--</option>
</select>

JS Code:
Copy Code code as follows:

<script type= "Text/javascript" >
$.ajaxsetup ({
Cache:false
}); Clear the previous values in Ajax
function Dochange (obj) {
var url = ' <c:url value= '/nutrition/getsubdictdatatojsonbycategory.action '/> ' + ' rawfood.category= ' +obj;// The first Drop-down box is selected and entered in the Action,action to assign the value required for the second Drop-down box
$.getjson (Url,function (JSON) {
var Seleojb=document.getelementbyid ("Subselect");
for (Var k=0;k<seleojb.options.length;k++) {
SeleOjb.options.remove (k); When you select the value in the first Drop-down box, clear the original value from the second Drop-down box
}
$.each (Json.subcategorydictlist,function (index,subdictdata) {
var optionj=document.createelement ("option"); Assign a value to the second Drop-down box
Optionj.value = subdictdata.id;
alert (subdictdata.id);
Optionj.text=subdictdata.name;
Seleojb.add (OPTIONJ);
});
});
}
</script>

Copy Code code as follows:

<action name= "getsubdictdatatojsonbycategory" class= "Rawfoodaction" method= "Getsubdictdatatojsonbycategory" >
<result name= "Success" type= "JSON" >
<param name= " Includeproperties ">//define returned parameters
^subcategorydictlist\[\d+\]\.id,
^subcategorydictlist\[\d+\]\.name
& Lt;/param>
</result>
</action>
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.