Today, I encountered secondary linkage when writing a project. During this period, I encountered some problems. I wrote a blog to record it.
Backstage Controller:
@RequestMapping("/faultType")@ResponseBodypublic Map<String,Object> faultType(int id,HttpServletRequest request)throws IOException{String ReturnMessage = "";//Get all subclass fault types List<FaultType> fauList=faultTypeService.getById(id); if(fauList.size()>0){request.setAttribute("childType", fauList);ReturnMessage = "OK";}else {ReturnMessage = "Information not found";}//************************************************************************Map<String,Object> ReturnMAP = new HashMap<String,Object>();ReturnMAP.put("childType", fauList);return ReturnMAP;}Front desk JSP:
<div><div><label>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><label>Fault: </label><input id="childTypeCont" name="childTypeCont" value="" type="hidden" placeholder="fault"><select name="faulttype1" id="faulttype1""><option>--Please select--</option><c:forEach items="${childType }" var="faulist"><option value="${faulist.faultId }">${faulist.faultContent }</option></c:forEach></select></select></div>JS:
function typeChange(){var type=$("#faulttype").val();var html = "<option>--Please select--</option>"; var CommitUrl = "faultType.do?id=" + type;$.ajax( {type : "POST",contentType : "application/json",url : CommitUrl,dataType : '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 written by someone else (original address: http://blog.csdn.net/gis__/article/details/6647464)
If you have a bad memory, you can bookmark it:
1. Pull down box:
var cc1 = $(".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 = $('.formcelect[@name="country"]').val(); //Get the value of the selected item in the drop-down menu var cc3 = $('.formc select[@name="country"]').attr("id");//Get the ID attribute value of the selected item in the drop-down menu $("#select").empty();//Clear the drop-down box//$("#select").html(''); $("<optionvalueoptionvalue='1'>1111</option>").appendTo("#select")//Add the option of the drop-down boxLet me explain a little:
1.select[@name='country'] option[@selected] means that it has the name attribute,
And the option element with selected attribute in the select element with the value of the attribute is 'country';
It can be seen that having an @ start means that the attribute is followed by it.
2. Radio box:
$("input[@type=radio][@checked]").val(); //Get the value of the selected item of the radio box (note that there is no space in the middle) $("input[@type=radio][@value=2]").attr("checked",'checked'); //Set the radio box value=2 as the selected state. (Note that there is no space in the middle)3. Check box:
$("input[@type=checkbox][@checked]").val(); //Get the value of the first item selected in the check box $("input[@type=checkbox][@checked]").each(function(){//Because there are multiple check boxes, you can loop the output alert($(this).val()); }); $("#chk1").attr("checked",''); //Don't check $("#chk2").attr("checked",true); //Tick if($("#chk1").attr('checked')==undefined){} //Try whether it has been checkedOf course, jquery selector is powerful. There are many ways to do it.
<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.jqueryradio takes the value, checkbox takes the value, select takes the value, radio selects, checkbox is selected, selects, and related to obtain the values of a group of radio selected items
10.var item = $('input[@name=items][@checked]').val();
11. Get the text of the selected item
12.var item = $("select[@name=items] option[@selected]").text();
13.Select drop-down box has the second element.
14.$('#select_id')[0].selectedIndex = 1;
15. The second element of the radio radio group is the current selected value
16.$('input[@name=items]').get(1).checked = true;
17. Get the value:
18. Text box, text area: $("#txt").attr("value");
19. Multiple selection box checkbox: $("#checkbox_id").attr("value");
20.Single-choice group radio: $("input[@type=radio][@checked]").val();
21. Pull-down box select: $('#sel').val();
22. Control form elements:
23. Text box, text area: $("#txt").attr("value",'');//Clear content
24. $("#txt").attr("value",'11');//Fill in content
25. Multiple checkbox checkbox: $("#chk1").attr("checked",'');//Don't check
26. $("#chk2").attr("checked",true);//Tick
27. if($("#chk1").attr('checked')==undefined) //Judge whether the check has been checked
28.Single-choice group radio: $("input[@type=radio]").attr("checked",'2');//The item with value=2 is set to be the currently selected item
29. Pull-down box select: $("#sel").attr("value",'-sel3');//Set the item with value=-sel3 to the currently selected item
30. $("<optionvalueoptionvalue='1'>1111</option><optionvalueoptionvalue='2'>2222</option>").appendTo("#sel")//Open
Add a drop-down box option
31. $("#sel").empty(); //Clear the drop-down box
32. Get the values of a set of radio selected items
33.var item = $('input[@name=items][@checked]').val();
34. Get the text of the selected item
35.var item = $("select[@name=items] option[@selected]").text();
36.Select drop-down box has the second element of the currently selected value
37.$('#select_id')[0].selectedIndex = 1;
38. The second element of the radio radio group is the current selected value
39.$('input[@name=items]').get(1).checked = true;
40. Get the value:
41. Text box, text area: $("#txt").attr("value");
42. Multiple selection box checkbox: $("#checkbox_id").attr("value");
43.Single-choice group radio: $("input[@type=radio][@checked]").val();
44. Pull-down box select: $('#sel').val();
45. Control form elements:
46. Text box, text area: $("#txt").attr("value",'');//Clear content
47.$("#txt").attr("value",'11');//Fill in the content
48. Multiple checkbox checkbox: $("#chk1").attr("checked",'');//Don't check
49.$("#chk2").attr("checked",true);//Tick
50.if($("#chk1").attr('checked')==undefined) //Judge whether the check has been checked
51.Single-choice group radio: $("input[@type=radio]").attr("checked",'2');//The item with value=2 is set to be the currently selected item
52. Pull down box select: $("#sel").attr("value",'-sel3');//Set the item with value=-sel3 to the currently selected item
53.$("<optionvalueoptionvalue='1'>1111</option><optionvalueoptionvalue='2'>2222</option>").appendTo("#sel")//Add the option of the drop-down box
54.$("#sel").empty(); //Clear the drop-down box
The above simple example of Ajax achieving secondary linkage in Spring MVC is all the content I share with you. I hope you can give you a reference and I hope you can support Wulin.com more.