Simple Example of Ajax Implementation of level-2 interaction in Spring MVC, mvcajax

Source: Internet
Author: User

Simple Example of Ajax Implementation of level-2 interaction in Spring MVC, mvcajax

Today, I encountered a second-level linkage when I was writing a project. I encountered some problems during this period and wrote a blog record.

Background Controller:

@ RequestMapping ("/faultType") @ ResponseBodypublic Map <String, Object> faultType (int id, HttpServletRequest request) throws IOException {String ReturnMessage = ""; // obtain the List of all subclass fault types <FaultType> fauList = faultTypeService. getById (id); if (fauList. size ()> 0) {request. setAttribute ("childType", fauList); ReturnMessage = "OK";} else {ReturnMessage = "No information found ";} //************************************** * ********************** Map <String, object> ReturnMAP = new HashMap <String, Object> (); ReturnMAP. put ("childType", fauList); return ReturnMAP ;}

Foreground JSP:

<Div class = "col-sm-3"> <div class = "form-group"> <label class = "col-3 control-label no-padding-right"> fault type: </label> <select name = "faulttype" id = "faulttype" onchange = "javascript: typeChange ()"> <c: forEach items = "$ {faultlist}" var = "faulist"> <option value = "$ {faulist. faultId} ">$ {faulist. faultContent} </option> </c: forEach> </select> </div> <div class = "col-sm-3"> <div class = "form-group"> <label class = "col-3 control-label no-padding-right "> fault: </label> <input id = "childTypeCont" name = "childTypeCont" value = "" type = "hidden" class = "col-sm-4 form-control" placeholder = "fault"> <select name = "faulttype1" id = "faulttype1" "> <option> -- select -- </option> <c: forEach items = "$ {childType}" var = "faulist"> <option value = "$ {faulist. faultId} ">$ {faulist. faultContent }</option> </c: forEach> </select> </div>

JS:

Function typeChange () {var type = $ ("# faulttype "). val (); var html = "<option> -- select -- </option>"; var CommitUrl = "faultType. do? Id = "+ type; $. ajax ({type: "POST", contentType: "application/json", url: CommitUrl, ype: 'json', success: function (result) {var Curedata = $. extend (true, [], result); if (Curedata. childType! = Null) {for (var I = 0; I <Curedata. childType. length; I ++) {html + = "<option value = '" + Curedata. childType [I]. faultId + "'>" + Curedata. childType [I]. faultContent + "</option>" ;}$ ("# faulttype1 "). empty (); $ (html ). appendTo ("# faulttype1 ");}}});}

The following is a reference written by someone else (original address: http://blog.csdn.net/gis__/article/details/6647464)

If your memory is poor, you can add it to your favorites:

1. drop-down box:

Var C0 = $ (". formcselect [@ name = 'country'] option [@ selected] "). text (); // get the text of the selected item in the drop-down menu (note that there is a space in the middle) var cc2 = $ ('. formcselect [@ name = "country"] '). val (); // obtain the value of the selected item from the drop-down menu var cc3 = $ ('. formc select [@ name = "country"] '). attr ("id"); // get the ID attribute value of the selected item from the drop-down menu $ ("# select "). empty (); // clear the drop-down box // $ ("# select" ).html (''); $ (" <optionvalueoptionvalue = '1'> 1111 </option> "). appendTo ("# select") // Add the option in the drop-down box

A little explanation:

1. select [@ name = 'country'] option [@ selected] indicates that the name attribute exists,

In addition, the select element with the selected attribute in the 'country' select element has the selected attribute;

It can be seen that the attribute is followed by the @ parameter.

2, single region:

$ ("Input [@ type = radio] [@ checked]"). val (); // get the value of the selected item of a single sequence (note that there is no space in the middle) $ ("input [@ type = radio] [@ value = 2]"). attr ("checked", 'checked'); // set single checked value = 2 to the selected status. (Note that there is no space in the middle)

3. Check box:

$ ("Input [@ type = checkbox] [@ checked]"). val (); // obtain the value of the first check box $ ("input [@ type = checkbox] [@ checked]"). each (function () {// Since multiple check boxes are selected, You can output alert ($ (this) cyclically ). val () ;}); $ ("# chk1 "). attr ("checked", ''); // do not tick $ (" # chk2 "). attr ("checked", true); // tick if ($ ("# chk1 "). attr ('checked') = undefined) {} // checks whether the check has been completed.

Of course, jquery's selector is powerful. There are many other methods.

<script src="jquery-1.2.1.js"type="text/javascript"></script> <script language="javascript"type="text/javascript"> $(document).ready(function(){ $("#selectTest").change(function() {    //alert("Hello");    //alert($("#selectTest").attr("name"));   //$("a").attr("href","xx.html");    //window.location.href="xx.html";   //alert($("#selectTest").val());    alert($("#selectTest option[@selected]").text());    $("#selectTest").attr("value", "2"); }); }); </script>  <ahrefahref="#">aaass</a> 

<! -- Drop-down box -->

1. <select id = "selectTest" name = "selectTest">
2. <optionvalueoptionvalue = "1"> 11 </option>
3. <optionvalueoptionvalue = "2"> 22 </option>
4. <optionvalueoptionvalue = "3"> 33 </option>
5. <optionvalueoptionvalue = "4"> 44 </option>
6. <optionvalueoptionvalue = "5"> 55 </option>
7. <optionvalueoptionvalue = "6"> 66 </option>
8. </select>

9. select jqueryradio, checkbox, select, radio, checkbox, and select to obtain the values of a set of radio selected items.

10. var item = $ ('input [@ name = items] [@ checked] '). val ();

11. Get the text of the selected select item

12. var item = $ ("select [@ name = items] option [@ selected]"). text ();

13. The second element in the select drop-down box is the selected value.

14. $ ('# select_id') [0]. selectedIndex = 1;

15. The second element of the radio Group is the currently selected value.

16. $ ('input [@ name = items] '). get (1). checked = true;

17. Get value:

18. text box, text area: $ ("# txt"). attr ("value ");

19. Multi-choice box checkbox: $ ("# checkbox_id"). attr ("value ");

20. radio Group radio: $ ("input [@ type = radio] [@ checked]"). val ();

21. select: $ ('# sel'). val ();

22. Control form elements:

23. text box, text area: $ ("# txt"). attr ("value", ''); // clear the content

24. $ ("# txt"). attr ("value", '11'); // fill in the content

25. Multi-choice box checkbox: $ ("# chk1"). attr ("checked", ''); // do not check

26. $ ("# chk2"). attr ("checked", true); // check

27. if ($ ("# chk1"). attr ('checked') = undefined) // you can check whether the checked has been checked.

28. radio Group radio: $ ("input [@ type = radio]"). attr ("checked", '2'); // set the project with value = 2 as the currently selected item

29. drop-down box select: $ ("# sel"). attr ("value", '-sel3'); // set the project with value =-sel3 as the selected item

30. $ ("<optionvalueoptionvalue = '1'> 1111 </option> <optionvalueoptionvalue = '2'> 2222 </option> "). appendTo ("# sel") // Add

Option in the drop-down list

31. $ ("# sel"). empty (); // clear the drop-down list

32. Get the value of a set of radio selected items

33. var item = $ ('input [@ name = items] [@ checked] '). val ();

34. Obtain the text of the selected select item

35. var item = $ ("select [@ name = items] option [@ selected]"). text ();

36. The second element in the select drop-down box is the selected value.

37. $ ('# select_id') [0]. selectedIndex = 1;

38. The second element of the radio Group is the currently selected value.

39. $ ('input [@ name = items] '). get (1). checked = true;

40. Get value:

41. text box, text area: $ ("# txt"). attr ("value ");

42. Multiple choice box checkbox: $ ("# checkbox_id"). attr ("value ");

43. radio Group radio: $ ("input [@ type = radio] [@ checked]"). val ();

44. select: $ ('# sel'). val ();

45. Control form elements:

46. text box, text area: $ ("# txt"). attr ("value", ''); // clear the content

47. $ ("# txt"). attr ("value", '11'); // fill in the content

48. Multi-choice box checkbox: $ ("# chk1"). attr ("checked", ''); // do not check

49. $ ("# chk2"). attr ("checked", true); // check

50. if ($ ("# chk1"). attr ('checked') = undefined) // you can check whether the checked has been checked.

51. radio Group radio: $ ("input [@ type = radio]"). attr ("checked", '2'); // set the project with value = 2 as the currently selected item

52. drop-down box select: $ ("# sel"). attr ("value", '-sel3'); // set the project with value =-sel3 as the selected item

53. $ ("<optionvalueoptionvalue = '1'> 1111 </option> <optionvalueoptionvalue = '2'> 2222 </option> "). appendTo ("# sel") // Add the option in the drop-down box

54. $ ("# sel"). empty (); // clear the drop-down list

The simple example of Ajax Implementation of level-2 interaction in Spring MVC above is all the content shared by xiaobian. I hope to give you a reference and support for the customer's house.

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.