用jquery怎么实现checkbox级联

用jquery怎么实现checkbox级联,第1张

添加checkbox

protected void Page_Load(object sender, EventArgs e)

{

CheckBox chk = new CheckBox()

chk.Text = "testall" // 这里可以换成数据库的内容

chk.CheckedChanged += new EventHandler(chk_CheckedChanged)

chk.AutoPostBack = true

Page.Form.Controls.Add(chk)

for (int i = 0i <10i++)

{

CheckBox chk2 = new CheckBox()

chk2.Text = "test" + i.ToString() // 这里可以换成数据库的内容

chk2.Checked = (i % 3 == 0)// 这里可以换成数据库的内容

Page.Form.Controls.Add(chk2)

}

}

void chk_CheckedChanged(object sender, EventArgs e)

{

CheckBox all = sender as CheckBox

foreach(Control ctl in Page.Form.Controls)

{

if (ctl is CheckBox)

{

CheckBox chk = ctl as CheckBox

chk.Checked = all.Checked

}

}

}

拼了!为了这200分。我刚搜索了一下,居然没找到四级联动

我准备今天花一小时为你写一个无级联动!!!就是随便你增加多少,都不要改程序

------------------------------------------------------

<!DOCTYPE>

<html>

<head>

<meta http-equiv="Content-Type" content="text/htmlcharset=utf-8" />

<title>无极联动啊,花了我一个小时</title>

<script src="这里是 jQuery.js,你自己加载" type="text/javascript"></script>

</head>

<body>

<div id="form">

一级<select><option value="">请选择</option><option value="10">10</option><option value="20">20</option><option value="30">30</option></select>

二级<select></select>

三级<select></select>

四级<select></select>

</div>

<script type="text/javascript">

var url = 'data.php'

var defaultOption = '<option>请选择</option>'

$('#form select').each(function(){

//$(this).prepend('<option>请选择</option>')

}).change(function(){

var $opt = $(this)

var index = $('#form select').index(this)

if( $(this).val() == '' )

return

$.get( url ,{opt:$(this).val(),index:index},function(data){

var showData = eval('('+data+')')

$opt.nextAll('select').add(this).each(function(i){

var html = ''

var value = ''

for( var j = 0j <showData.length j++ ){

if( typeof( showData[i] ) == 'undefined' || typeof( showData[i][j]) == 'undefined' )

continue

html += '<option vlaue="'+ showData[i][j] + '">'+showData[i][j]+ '</option>'

}

$(this).html( html )

})

})

})

</script>

</body>

</html>

------------------------------------------

这是测试数据,实际情况,你是从数据库调用的,我写了一个demo

-----------------------------------------

data.php

<?php

$opt = $_GET['opt']

$index = $_GET['index']

if( $index = '1' ){

if( $opt == 10 )

exit('Array([1021,1022,1032,1024],[1031,1032,1033,1034],[1041,1042,1043,1044])')

elseif( $opt == 20 )

exit('Array([2021,2022,2032,1024],[2031,2032,2033,1034],[2041,2042,2043,2044])')

elseif( $opt == 30 )

exit('Array([3021,3022,3032,3024],[3031,3032,3033,3034],[3041,1042,3043,3044])')

}elseif( $index = '2' ){

// 这里的数据是一样的格式。。。。

}if( $opt = '3' ){

//...

}

?>

-------------------------------

我只做了第几级的数据哦,这是无极联动,你直接增加数据就可以了,不需要修改程序。

累死我了,吃饭去了。

有问题留言。

解决方式:源码稍作修改可以解决这个问题!

原理 :在过滤记录集的时候更新下缓存!

源码中找到 filterResults 的定义处,其中增加一句: this.preloadCache()


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/bake/11555761.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-17
下一篇 2023-05-17

发表评论

登录后才能评论

评论列表(0条)

保存