jquery自动填充勾选框

本文介绍如何使用jQuery结合Ajax技术实现网页中下拉菜单的动态加载及复选框的自动填充功能。具体包括:通过下拉框选择触发事件,使用Ajax请求后台数据,解析返回的数据并为相应的复选框设置选中状态。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

jquery自动填充勾选框,即把勾选框打上(true),然后通过ajax方式获得勾选项列表,再把列表内的选项打上。

下拉框<select name="makemodule" id="makemodule" style='width:130px' onchange='makemoduleSelected()'>
	<option value='1'>1</option>
</select>


select改变,触发函数makemoduleSelected(),该函数如下:

//模板下拉框发生变化时,触发此事件(onchange)。
	function makemoduleSelected(){
		clearAll('property');
		var modtitlecode = $("#makemodule").val();
		$.ajax({
			url	: 'indexStatisticsAction_getSelect.jsp',
			data: { page:'clientindexStatistics.jsp',method:'get_subname_en',modtitlecode:modtitlecode},
			success : function(result){
				// 根据result返回信息判断是否登录成功
				var results = result.split(",");
				//document.getElementById(results[i]).checked = true;
				$(".indexStatistics").each(function(){
					$(this).find("input").each(function(){
						var tempVal = $(this).val();
			           		 for(var i=0; i<results.length; i++){
				            		if(tempVal == results[i]) $(this).attr("checked", true); 
			           	 	 }
					});
				});
			}
		});
	}


该函数通过ajax方式向indexStatisticsAction_getSelect.jsp发出请求,返回一个字符串,把改字符串分开成字符串数组,接下来遍历标签<div class="indexStatistics">下面的标签,遇到相关的标签,则打钩(true)。indexStatisticsAction_getSelect.jsp的相关代码如下:

//获取模板对应的指标
	if(method.equals("get_subname_en")){
		String modtitlecode = request.getParameter("modtitlecode");
		if(modtitlecode.equals("-------")) return;
		String sql = sql2.replace("?modtitlecode?",modtitlecode);
		sql = sql.replace("?userId?",userId);
		System.out.println(sql);
		StringBuffer subnames = new StringBuffer();
		Db db = new Db();
		try {
			db.prepareQuery();
			ResultSet rs = db.executeQuery(sql);
			while (rs!=null && rs.next()) {
				subnames.append(rs.getString("subname_en"));
				subnames.append(",");
			}
			rs.close();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			db.endQuery();
		}
		PrintWriter pout = response.getWriter();
		pout.write(subnames.toString().substring(0,subnames.length()-1));
		pout.flush();
		pout.close();
	}


 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值