<?xml version="1.0" encoding="UTF-8" ?>
<rss version="2.0">
<channel>
<title><![CDATA[刘新修]]></title> 
<link>http://liuxinxiu.com:80/index.php</link> 
<description><![CDATA[刘新修的个人博客 (Liuxinxiu'S Blog)]]></description> 
<language>zh-cn</language> 
<copyright><![CDATA[刘新修]]></copyright>
<item>
<link>http://liuxinxiu.com:80/CheckBox/</link>
<title><![CDATA[JQuery对CheckBox的一些相关操作]]></title> 
<author>刘新修 &lt;admin@yourname.com&gt;</author>
<category><![CDATA[H5/JS/CSS]]></category>
<pubDate>Sat, 13 Sep 2014 00:12:19 +0000</pubDate> 
<guid>http://liuxinxiu.com:80/CheckBox/</guid> 
<description>
<![CDATA[ 
	<p>&nbsp;一、对CheckBox的操作：</p><div>&nbsp; &nbsp;以这段checkBox代码为例：</div><div>&nbsp;</div><div>&nbsp; &nbsp;&lt;input type=&quot;checkbox&quot; name=&quot;box&quot; &nbsp;value=&quot;0&quot; checked=&quot;checked&quot; /&gt;&nbsp;</div><div>&nbsp; &nbsp;&lt;input type=&quot;checkbox&quot; name=&quot;box&quot; value=&quot;1&quot; /&gt;</div><div>&nbsp; &nbsp;&lt;input type=&quot;checkbox&quot; name=&quot;box&quot; value=&quot;2&quot; /&gt;</div><div>&nbsp; &nbsp;&lt;input type=&quot;checkbox&quot; name=&quot;box&quot; value=&quot;3&quot; /&gt;</div><div>&nbsp; &nbsp;1.遍历checkbox用each()方法：</div><div>&nbsp; &nbsp; &nbsp; &nbsp;$(&quot;input[name='box']&quot;).each(function()&#123;&#125;);</div><div>&nbsp; &nbsp;2.设置checkbox被选中用attr();方法：</div><div>&nbsp; &nbsp; &nbsp;$(&quot;input[name='box']&quot;).attr(&quot;checked&quot;,&quot;checked&quot;);&nbsp;</div><div>&nbsp; &nbsp; 在HTML中，如果一个复选框被选中，对应的标记为 checked=&quot;checked&quot;。 但如果用jquery alert($(&quot;#id&quot;).attr(&quot;checked&quot;)) 则会提示您是&quot;true&quot;而不是&quot;checked&quot;，所以判断 if(&quot;checked&quot;==$(&quot;#id&quot;).attr(&quot;checked&quot;)) 是错误的，应该是 if(true == $(&quot;#id&quot;).attr(&quot;checked&quot;))</div><div>&nbsp; 3.获取被选中的checkbox的值：</div><div>&nbsp; &nbsp; $(&quot;input[name='box'][checked]&quot;).each(function()&#123;</div><div>&nbsp; &nbsp; if (true == $(this).attr(&quot;checked&quot;)) &#123;</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; alert( $(this).attr('value') );</div><div>&nbsp; &nbsp; &#125;</div><div>&nbsp; &nbsp; &nbsp;或者：</div><div>&nbsp; &nbsp; $(&quot;input[name='box']：checked&quot;).each(function()&#123;</div><div>&nbsp; &nbsp; if (true == $(this).attr(&quot;checked&quot;)) &#123;</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; alert( $(this).attr('value') );</div><div>&nbsp; &nbsp; &#125;</div><div>&nbsp; &nbsp;$(&quot;input[name='box']：checked&quot;)与 $(&quot;input[name='box']&quot;)有何区别没试过，我试了用 $(&quot;input[name='box']&quot;)能成功。</div><div>&nbsp; &nbsp;4.获取未选中的checkbox的值：</div><div>&nbsp;</div><div>&nbsp; &nbsp; $(&quot;input[name='box']&quot;).each(function()&#123;</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if ($(this).attr('checked') ==false) &#123;</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; alert($(this).val());</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &#125;</div><div>&nbsp; &nbsp; &nbsp;&#125;);</div><div>&nbsp; &nbsp;5.设置checkbox的value属性的值:</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $(this).attr(&quot;value&quot;,值);</div><div>&nbsp;</div><div>二、 一般都是创建一个js数组来存储遍历checkbox得到的值，创建js数组的方法：</div><div>&nbsp; &nbsp; &nbsp;1. &nbsp;var array= new Array();</div><div>&nbsp; &nbsp; &nbsp;2. 往数组添加数据：</div><div>&nbsp; &nbsp; &nbsp; &nbsp; array.push($(this).val());</div><div>&nbsp; &nbsp; &nbsp;3.数组以&ldquo;,&rdquo;分隔输出：</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;alert(array.join(','));</div><div>&nbsp;</div><div>&nbsp; &lt;input type=&quot;checkbox&quot; name=&quot;myBox&quot; class=&quot;chkTwo&quot; value=&quot;2&quot; /&gt;</div><div>&nbsp; &lt;input type=&quot;checkbox&quot; name=&quot;myBox&quot; id=&quot;chkOne&quot; value=&quot;2&quot; /&gt;</div>
]]>
</description>
</item><item>
<link>http://liuxinxiu.com:80/CheckBox/#blogcomment</link>
<title><![CDATA[[评论] JQuery对CheckBox的一些相关操作]]></title> 
<author> &lt;user@domain.com&gt;</author>
<category><![CDATA[评论]]></category>
<pubDate>Thu, 01 Jan 1970 00:00:00 +0000</pubDate> 
<guid>http://liuxinxiu.com:80/CheckBox/#blogcomment</guid> 
<description>
<![CDATA[ 
	
]]>
</description>
</item>
</channel>
</rss>