求一个JAVAscript(搞定结) 一个form表单里面有很多checkbox 默认是全部选中怎么实现当我只选一部份时在提交时 还是我选的那部份(因为提交是本页面 只要一提交他就 默认是全部选中) 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 提交到本页面后,获得选中的值,把该值放到隐藏表单域中,在js中获得该值。加载的时候循环所有的checkbox和该值匹配,有的就设置成选中。 大概的个思路,希望对lz有帮助 <html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=gb2312" /><title>无标题文档</title><script type="text/javascript"> window.onload = function(){ document.getElementById('click').onclick = function(){ var oChecks = document.getElementsByTagName('input'); var result = ''; for(var i = 0; i < oChecks.length; i++){(function(obj,i){ if(obj.type == 'checkbox' && obj.checked){ result += obj.id + ' '; } })(oChecks[i],i)} alert('选中的ID有: ' + result); }; };</script></head><body> 1<input type="checkbox" id="1" checked="checked" /><br /> 2<input type="checkbox" id="2" checked="checked" /><br /> 3<input type="checkbox" id="3" checked="checked" /><br /> 4<input type="checkbox" id="4" checked="checked" /><br /> 5<input type="checkbox" id="5" checked="checked" /><br /> 6<input type="checkbox" id="6" checked="checked" /><br /> <input type="button" id="click" value="click" /></body></html>你是这意思吗? jquery 1.3 $("input[type=checkbox]").attr("checked",true);//打勾 要用到服务器记录状态,回发的时候重新初始化被选中的 checkbox要不用Ajax 技术做异步提交。 这种现象是由于提交form后,页面刷新了,所以checkbox又回到了初始状态。解决思路就像LS所说,或者提交后后台记录,在返回前台时根据数据在onload时重设checkbox的状态;或者用不刷新页面的方法如用ajax提交,或设置form的target为一iframe等方法。 <html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=gb2312" /><title>test</title><script type="text/javascript">var arr=null;function toury(f){ var s=''; for(var i=0;i<arr.length;i++){ if(arr[i].checked){s=s==''?arr[i].id:s+"|"+arr[i].id} } var url=location.href; if(url.lastIndexOf('s=')!=-1){ url=url.split("s=")[0];//防止多次提交时地址中重复包含s参数 url=url.substr(0,url.length-1) } url +=url.lastIndexOf('?')==-1?"?s=":"&s="; f.action=url+s; f.submit();}window.onload = function(){ arr=document.getElementsByName('c'); if(location.href.lastIndexOf('s=')==-1)return; var s=location.href.split("s=")[1]; for(var i=0;i<arr.length;i++){arr[i].checked=s.indexOf(arr[i].id)==-1?false:true;}}</script></head><body><form method=post action='' name=frm onsubmit='toury(this)'> <input type="checkbox" id="c0" name='c' checked="checked" /><br /> <input type="checkbox" id="c1" name='c' checked="checked" /><br /> <input type="checkbox" id="c2" name='c' checked="checked" /><br /> <input type="checkbox" id="c3" name='c' checked="checked" /><br /> <input type="checkbox" id="c4" name='c' checked="checked" /><br /> <input type="checkbox" id="c5" name='c' checked="checked" /><br /> <input type="submit" id="submit" name="submit" value="提交" /></form></body> 获得国际黄金价格数字的代码 万能JQuery弹出层类库的bug修正,提供新的demo JS如何过滤空格 firefox下删除cookie无效 遇到鬼了?谁帮测试一下 过滤标签,控制样式,这个问题该怎么解决 :( 页面载入完了,状态栏怎么还在读 求一个日期正则表达式 JS如何判断是否已经引入某个css或是js? 哪里有javascript经典的教程? JavaScript取DATALIST内所有A标记 div 如何在object上面 。
大概的个思路,希望对lz有帮助
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>无标题文档</title>
<script type="text/javascript">
window.onload = function(){
document.getElementById('click').onclick = function(){
var oChecks = document.getElementsByTagName('input');
var result = '';
for(var i = 0; i < oChecks.length; i++){(function(obj,i){
if(obj.type == 'checkbox' && obj.checked){
result += obj.id + ' ';
}
})(oChecks[i],i)}
alert('选中的ID有: ' + result);
};
};
</script>
</head><body>
1<input type="checkbox" id="1" checked="checked" /><br />
2<input type="checkbox" id="2" checked="checked" /><br />
3<input type="checkbox" id="3" checked="checked" /><br />
4<input type="checkbox" id="4" checked="checked" /><br />
5<input type="checkbox" id="5" checked="checked" /><br />
6<input type="checkbox" id="6" checked="checked" /><br />
<input type="button" id="click" value="click" />
</body>
</html>
你是这意思吗?
$("input[type=checkbox]").attr("checked",true);//打勾
要不用Ajax 技术做异步提交。
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>test</title>
<script type="text/javascript">
var arr=null;
function toury(f){
var s='';
for(var i=0;i<arr.length;i++){ if(arr[i].checked){s=s==''?arr[i].id:s+"|"+arr[i].id} }
var url=location.href;
if(url.lastIndexOf('s=')!=-1){
url=url.split("s=")[0];//防止多次提交时地址中重复包含s参数
url=url.substr(0,url.length-1)
}
url +=url.lastIndexOf('?')==-1?"?s=":"&s=";
f.action=url+s; f.submit();
}window.onload = function(){ arr=document.getElementsByName('c');
if(location.href.lastIndexOf('s=')==-1)return;
var s=location.href.split("s=")[1];
for(var i=0;i<arr.length;i++){arr[i].checked=s.indexOf(arr[i].id)==-1?false:true;}
}</script>
</head><body>
<form method=post action='' name=frm onsubmit='toury(this)'>
<input type="checkbox" id="c0" name='c' checked="checked" /><br />
<input type="checkbox" id="c1" name='c' checked="checked" /><br />
<input type="checkbox" id="c2" name='c' checked="checked" /><br />
<input type="checkbox" id="c3" name='c' checked="checked" /><br />
<input type="checkbox" id="c4" name='c' checked="checked" /><br />
<input type="checkbox" id="c5" name='c' checked="checked" /><br />
<input type="submit" id="submit" name="submit" value="提交" />
</form>
</body>