JSP二级下拉框的一种简单写法
<%—
**全选** **选择分类**
—%>var checkboxValue=””;
var checkboxText=””;
var checkboxOneValue=new Array();
var checkboxTwoValue=new Array();
var checkboxOneText=new Array();
var checkboxTwoText=new Array();
var checkboxOneStr=document.getElementsByName(“opinionId”);
var checkboxTwoStr=document.getElementsByName(“opinionChildId”);
var opinionList=${requestScope.opinion.opinion};
//debugger
for (let f=0; f<checkboxOneStr.length;f++) {
if(checkboxOneStr[f].checked){
//id
checkboxOneValue.push(checkboxOneStr[f].value);
//name
checkboxOneText.push(checkboxOneStr[f].nextSibling.nodeValue);
}
}
for (let f=0; f<checkboxTwoStr.length;f++) {
if(checkboxTwoStr[f].checked){
//id
checkboxTwoValue.push(checkboxTwoStr[f].value);
//name
checkboxTwoText.push(checkboxTwoStr[f].nextSibling.nodeValue);
}
}
//一级项二级项皆选中情况
for (let j = 0; j <checkboxOneValue.length ; j++) {
for (let i = 0; i <opinionList.length ; i++) {
if (opinionList[i].opinionId == checkboxOneValue[j]){
for (let k = 0; k < opinionList[i].children.length; k++) {
for (let m = 0; m < checkboxTwoValue.length; m++) {
if (opinionList[i].children[k].opinionChildId == checkboxTwoValue[m]){
checkboxText += opinionList[i].opinionName + “-“;
checkboxText +=checkboxTwoText[m] + “,”;
checkboxValue += checkboxTwoValue[m] + “,”;
}
}
}
}
}
}
//一级项未选中情况
for (let i = 0; i <opinionList.length ; i++) {
debugger
if(checkboxOneValue.indexOf(opinionList[i].opinionId+””)<0) {
for (let k = 0; k < opinionList[i].children.length; k++) {
for (let m = 0; m < checkboxTwoValue.length; m++) {
if (opinionList[i].children[k].opinionChildId == checkboxTwoValue[m]) {
checkboxText += opinionList[i].opinionName + “-“;
checkboxText +=checkboxTwoText[m] + “,”;
checkboxValue += checkboxTwoValue[m] + “,”
}
}
}
}
}
checkboxValue = (checkboxValue.substring(checkboxValue.length - 1) == ‘,’) ? checkboxValue.substring(0, checkboxValue.length - 1) : checkboxValue;
checkboxText = (checkboxText.substring(checkboxText.length - 1) == ‘,’) ? checkboxText.substring(0, checkboxText.length - 1) : checkboxText;
$(“#opinionIds”).val(checkboxValue);
$(“#opinionNames”).val(checkboxText)
**<input type=”hidden” id=”opinionNames” name=”opinionNames” value=””/
//点击一级项全选中该一级项下的二级项
$(function(){
$(“input[name=’opinionId’]”).on(“change”,function (event) {
if($(event.target)[0].checked){
$(event.target).parent().siblings().find(“input[name=’opinionChildId’]”).attr(“checked”,true);
}else{
$(event.target).parent().siblings().find(“input[name=’opinionChildId’]”).attr(“checked”,false);
}
});
$(“input[name=’opinionChildId’]”).on(“change”,function (event) {
if($(event.target)[0].checked){
}else{
$(event.target).parents(“.h2_cat”).find(“input[name=’opinionId’]”).attr(“checked”,false);
}
});
})
效果图
//数据绑定
ids=event_classify_ids.split(“,”);
var opinionList=${requestScope.opinion.opinion
$(“#nature”).val(grade);
$(“#regionId”).val(regionId);
for (let m = 0; m
for (let k = 0; k < opinionList[i].children.length; k++) {
if (opinionList[i].children[k].opinionChildId == ids[m]) {
debugger
$(“#asd”+ids[m]+””).attr(“checked”,”true”)
}
}
}
}
}