下拉框联动 类似于请求第一个框之后,携带参数请求后端接口,渲染第二个下来框


直接上代码

复制代码
<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title>1233</title>
	</head>
	<body>
		<div>
			<!-- <table>
				  <tr>
				  <td>
					<div class="mdui-col">
						<input>
						<select id="mySelect">
						   <option value="option1">选项1</option>
						   <option value="option2">选项2</option>
						   <option value="option3">选项3</option>
						</select>
					</div>
					</td>
					
					<td>
					 >   
					</td>
					
					<td>
					<div class="mdui-col">
					<input>
					</div>
					</td>
					</tr>		
			</table> -->
			
			
			   <div class="mdui-row-xs-5">
									<div class="mdui-col">
			                                <div class="label">产品分类</div>
			                                <div class="infotext">
			                                    <select name="id" id="typeZero" class="face-select"><option value="" selected="">---</option>
			                                        <option value="1">微波炉</option><option value="2">烤箱</option><option value="3">3L</option></select>
			                                 &gt;
											</div>
											
											</div>
											
											<div class="mdui-col">
			                               
			                                <div class="infotext">
			                                    <select name="id" id="typeOne" class="face-select"><option value="" selected="">---</option></select>
												 &gt;
			                                </div>
											</div>
											
											<div class="mdui-col">
			                               
			                                <div class="infotext">
			                                    <select name="id" id="typeTwo" class="face-select"><option value="" selected="">---</option></select>
												 &gt;
			                                </div>
											</div>
											
											<div class="mdui-col">
			                              
			                                <div class="infotext">
			                                    <select name="id" id="typeThree" class="face-select"><option value="" selected="">---</option></select>
												 &gt;
			                                </div>
											</div>
			                                
											<div class="mdui-col">
											
			                                <div class="infotext">
			                                    <select name="id" id="typeFour" class="face-select"><option value="" selected="">---</option></select>
												 &gt;
			                                </div>
			                            </div>
			</div>
			
		 <!-- <label for="leftSelect">左侧选项</label>
		   <select id="leftSelect" multiple size="5"></select>
		   
		   <button type="button" class="moveRightBtn">>>></button>
		   <br><br>
		   
		   <label for="rightSelect">右侧选项</label>
		   <select id="rightSelect" multiple size="5"></select> -->
		</div>
	</body>
</html>


<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script>

$(document).ready(function() {
   // 初始化左边选项列表
   var leftOptions = ["Option 1", "Option 2", "Option 3"];
   $("#leftSelect").append($("<option/>").text("None"));
   $.each(leftOptions, function(index, value){
      $("#leftSelect").append($("<option/>").attr('value', index+1).text(value));
   });
   
   // 点击">>"按钮将左边选中的选项移动到右边
   $(".moveRightBtn").click(function(){
      $('#leftSelect option:selected').remove().appendTo('#rightSelect');
   });
});



$(document).ready(function () {
    $('#typeZero').change(function () {
	
    // $('#typeZero').click(function () {
        // 在这里编写点击事件的处理逻辑
        console.log('下拉框被点击了');
        // 获取当前选中的值
        var typeZero = $(this).val();
        console.log('当前选中的值是:' + typeZero);
		
	
		  
	var res = [{"id":"1","name":"2"},{"id":"zhangsan","name":"lisi"}]
  
                    // 清空原有的下拉框内容
                    $('#typeOne').empty();

                    // 动态生成option元素并添加到下拉框中
      //               for (let i = 0; i < res.length; i++) {
						// debugger
      //                   var option = $('<option>---</option>');
      //                   // option.text( res.length[i].name); // 设置每个option的显示文本
      //                   option.val( res.length[i].id); // 设置每个option对应的值(如果有)
      //                   $('#typeOne').append(option);
      //               }
					
					
					//
					// $.each(res, function(i) {
					// 	// debugger
					//     console.log(res[i]);    
					// 		    console.log(res[i].name);    
					// 			 console.log(res[i].id);    
					// }); 
				
				// $('#typeZero').empty();
					
					//正确的
					$('#typeOne').empty();
					$.each(res, function(i) {
						var option = $('<option>---</option>');
						option.text(res[i].name); // 设置每个option的显示文本
						// option.val(res.[i].id); // 设置每个option对应的值(如果有)
						option.val(res[i].id); 
						$('#typeOne').append(option);
					}); 
					
					
	
				// $('#typeOne').empty();
				// var option = $('<option>---</option>');
				// var option1 = $('<option>---</option>');
				// $.each(res, function(i) {
					
				// 	option1.text(res[i].name); // 设置每个option的显示文本
				// 	// option.val(res.[i].id); // 设置每个option对应的值(如果有)
				// 	option1.val(res[i].id); 
				// 	option+=option1;
				// }); 
				// $('#typeOne').append(option1);
				
					
					
					// res.forEach(function (val) {
					// 	debugger
					//                 console.log(val)
					//             });
					
               
            }
        );
    });


	</script>

具体代码

复制代码
$(document).ready(function () {
    $('#typeZero').change(function () {
        // 在这里编写点击事件的处理逻辑
        console.log('下拉框被点击了');
        // 获取当前选中的值
        var typeZero = $(this).val();
        console.log('当前选中的值是:' + typeZero);
        var params = {
            ID: typeZero,
            LEVEL: '1'
        };
        // 根据需要进行其他操作...
        //理解成请求的后端的接口的路径 完事了 ('productManage.ProductType@getType)
        f.doAjaxResult('productManage.ProductType@getType"', params,
            function (res) {
                console.log('请求结果:' + res);
                if (res.TYPE != null && res.TYPE != undefined && res.TYPE != '') {
                    // 清空原有的下拉框内容
                    $('#typeOne').empty();

                    // 动态生成option元素并添加到下拉框中
                    // for (let i = 0; i < res.TYPE.length; i++) {
                    //     var option = $('<option>---</option>');
                    //     option.text(res.TYPE[i].t_name); // 设置每个option的显示文本
                    //     option.val(res.TYPE[i].id); // 设置每个option对应的值(如果有)
                    //     $('#typeOne').append(option);
                    // }

                    $.each(res.TYPE, function(i) {
                        var option = $('<option>---</option>');
                        option.text(res.TYPE[i].t_name); // 设置每个option的显示文本
                        option.val(res.TYPE[i].id); // 设置每个option对应的值(如果有)
                        $('#typeOne').append(option);
                    });

                }
            }
        );
    });


    $('#typeOne').change(function () {
        // 在这里编写点击事件的处理逻辑
        console.log('下拉框被点击了');
        // 获取当前选中的值
        var typeZero = $(this).val();
        console.log('当前选中的值是:' + typeZero);
        var params = {
            ID: typeZero,
            LEVEL: '1'
        };
        // 根据需要进行其他操作...
        f.doAjaxResult('productManage.ProductType@getType"', params,
            function (res) {
                console.log('请求结果:' + res);
                if (res.TYPE != null && res.TYPE != undefined && res.TYPE != '') {
                    // 清空原有的下拉框内容
                    $('#typeTwo').empty();

                    $.each(res.TYPE, function(i) {
                        var option = $('<option>---</option>');
                        option.text(res.TYPE[i].t_name); // 设置每个option的显示文本
                        option.val(res.TYPE[i].id); // 设置每个option对应的值(如果有)
                        $('#typeTwo').append(option);
                    });
                }
            }
        );
    });


    $('#typeTwo').change(function () {
        // 在这里编写点击事件的处理逻辑
        console.log('下拉框被点击了');
        // 获取当前选中的值
        var typeZero = $(this).val();
        console.log('当前选中的值是:' + typeZero);
        var params = {
            ID: typeZero,
            LEVEL: '1'
        };
        // 根据需要进行其他操作...
        f.doAjaxResult('productManage.ProductType@getType"', params,
            function (res) {
                console.log('请求结果:' + res);
                if (res.TYPE != null && res.TYPE != undefined && res.TYPE != '') {
                    // 清空原有的下拉框内容
                    $('#typeThree').empty();

                    $.each(res.TYPE, function(i) {
                        var option = $('<option>---</option>');
                        option.text(res.TYPE[i].t_name); // 设置每个option的显示文本
                        option.val(res.TYPE[i].id); // 设置每个option对应的值(如果有)
                        $('#typeThree').append(option);
                    });
                }
            }
        );
    });

    $('#typeThree').change(function () {
        // 在这里编写点击事件的处理逻辑
        console.log('下拉框被点击了');
        // 获取当前选中的值
        var typeZero = $(this).val();
        console.log('当前选中的值是:' + typeZero);
        var params = {
            ID: typeZero,
            LEVEL: '1'
        };
        // 根据需要进行其他操作...
        f.doAjaxResult('productManage.ProductType@getType"', params,
            function (res) {
                console.log('请求结果:' + res);
                if (res.TYPE != null && res.TYPE != undefined && res.TYPE != '') {
                    // 清空原有的下拉框内容
                    $('#typeFour').empty();
                    $.each(res.TYPE, function(i) {
                        var option = $('<option></option>');
                        option.text(res.TYPE[i].t_name); // 设置每个option的显示文本
                        option.val(res.TYPE[i].id); // 设置每个option对应的值(如果有)
                        $('#typeFour').append(option);
                    });
                }
            }
        );
    });

});
相关推荐
一只小青团1 小时前
Python之面向对象和类
java·开发语言
qq_529835352 小时前
ThreadLocal内存泄漏 强引用vs弱引用
java·开发语言·jvm
景彡先生2 小时前
C++并行计算:OpenMP与MPI全解析
开发语言·c++
LuciferHuang3 小时前
震惊!三万star开源项目竟有致命Bug?
前端·javascript·debug
GISer_Jing3 小时前
前端实习总结——案例与大纲
前端·javascript
天天进步20153 小时前
前端工程化:Webpack从入门到精通
前端·webpack·node.js
量子联盟3 小时前
原创-基于 PHP 和 MySQL 的证书管理系统,免费开源
开发语言·mysql·php
姑苏洛言4 小时前
编写产品需求文档:黄历日历小程序
前端·javascript·后端
知识分享小能手4 小时前
Vue3 学习教程,从入门到精通,使用 VSCode 开发 Vue3 的详细指南(3)
前端·javascript·vue.js·学习·前端框架·vue·vue3
姑苏洛言5 小时前
搭建一款结合传统黄历功能的日历小程序
前端·javascript·后端