<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>網絡暴民 Jacky&#039;s Blog &#187; jquery</title>
	<atom:link href="http://jacky.seezone.net/tag/jquery/feed/" rel="self" type="application/rss+xml" />
	<link>http://jacky.seezone.net</link>
	<description></description>
	<lastBuildDate>Mon, 30 Jan 2012 17:45:38 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>My attempt to IE6/7 select box width</title>
		<link>http://jacky.seezone.net/2011/03/14/3335/</link>
		<comments>http://jacky.seezone.net/2011/03/14/3335/#comments</comments>
		<pubDate>Mon, 14 Mar 2011 08:31:17 +0000</pubDate>
		<dc:creator>Jacky</dc:creator>
				<category><![CDATA[電腦]]></category>
		<category><![CDATA[ie]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[jquery]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[ui]]></category>

		<guid isPermaLink="false">http://jacky.seezone.net/?p=3335</guid>
		<description><![CDATA[In IE6/7, when you set the width of a &#60;select&#62; element, it will chop off the option text. It&#8217;s an age-old question but I would like to record my attempt to this problem: create an inline block to wrap the &#60;select&#62; up set width and height of the inline block the same as the original [...]]]></description>
			<content:encoded><![CDATA[	<p>In IE6/7, when you set the width of a &lt;select&gt; element, it will chop off the option text. It&#8217;s an age-old question but I would like to record my attempt to this problem:</p>

	<ul>
		<li>create an inline block to wrap the &lt;select&gt; up</li>
		<li>set width and height of the inline block the same as the original select</li>
		<li>set position &#8216;relative&#8217; for inline block</li>
		<li>set position &#8216;absolute&#8217; for &lt;select&gt;</li>
		<li>when mouse over, set &lt;select&gt; width as &#8216;auto&#8217;</li>
		<li>when mouse click, toggle a flag &#8216;clicked&#8217; (using $.data)</li>
		<li>when mouse out and flagged as &#8216;clicked&#8217;, reset &lt;select&gt; width</li>
		<li>when blur or change, reset &lt;select&gt; width</li>
	</ul>

	<p>The reason for wrapping an inline block is that when &lt;select&gt; set/reset width, it would not affect the layout of other elements.</p>

	<p>The &#8216;clicked&#8217; flag is used to indicate if the &lt;select&gt; is &#8216;expanded&#8217;. If it is, the mouseout event should not reset the width.</p>

	<p>Here is the code (and of course only effective in IE6/7):</p>

	<p><iframe style="width: 500px; height: 500px" src="http://jsfiddle.net/jackysee/4N6w8/2/embedded/"></iframe></p>


	Tags:  <a href="http://jacky.seezone.net/tag/ie/" title="ie" rel="tag">ie</a>, <a href="http://jacky.seezone.net/tag/javascript/" title="javascript" rel="tag">javascript</a>, <a href="http://jacky.seezone.net/tag/jquery/" title="jquery" rel="tag">jquery</a>, <a href="http://jacky.seezone.net/tag/programming/" title="programming" rel="tag">programming</a>, <a href="http://jacky.seezone.net/tag/ui/" title="ui" rel="tag">ui</a><br />
]]></content:encoded>
			<wfw:commentRss>http://jacky.seezone.net/2011/03/14/3335/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Browser Feature Detection</title>
		<link>http://jacky.seezone.net/2010/01/13/2760/</link>
		<comments>http://jacky.seezone.net/2010/01/13/2760/#comments</comments>
		<pubDate>Wed, 13 Jan 2010 03:03:23 +0000</pubDate>
		<dc:creator>Jacky</dc:creator>
				<category><![CDATA[電腦]]></category>
		<category><![CDATA[browser]]></category>
		<category><![CDATA[ie]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[jquery]]></category>

		<guid isPermaLink="false">http://jacky.seezone.net/?p=2760</guid>
		<description><![CDATA[Often when dealing with different browsers, this is something I encounter recently (with jQuery): [js] var ie6 = $.browser.msie &#038;&#038; $.browser.version == '6.0'; if(ie6){ $('html,body').css({height:'100%',width:'100%'}); target.css('position','absolute'); //IE6 has no 'fixed' position //.... } [/js] This code is copied from a library which aims at create an overlay for modal box. From IE7 onwards, CSS style [...]]]></description>
			<content:encoded><![CDATA[	<p>Often when dealing with different browsers, this is something I encounter recently (with jQuery):</p>

[js]
var ie6 = $.browser.msie &#038;& $.browser.version == '6.0';
if(ie6){
	$('html,body').css({height:'100%',width:'100%'});
	target.css('position','absolute'); //IE6 has no 'fixed' position
	//....
}
[/js]

	<p>This code is copied from a library which aims at create an overlay for modal box. From IE7 onwards, <span class="caps">CSS</span> style position &#8216;fixed&#8217; is supported, but not in IE7&#8217;s quirks mode. So the above code would fail. The solution is let IE7 run at correct doctype. e.g.:</p>

[html]
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
 [/html]

	<p>But the code is still fragile to the environment. In jQuery, you can use $.support to get a set of feature detect properties. So, the code can become:</p>

[js]
if(!$.support.boxModel){
	$('html,body').css({height:'100%',width:'100%'});
	target.css('position','absolute'); 
	//....
}
[/js]

	<p>But it is still based on the assumption that &#8220;no support on boxModel = no support on fixed position&#8221;, which is &#8220;politically incorrect&#8221;. </p>
	Tags:  <a href="http://jacky.seezone.net/tag/browser/" title="browser" rel="tag">browser</a>, <a href="http://jacky.seezone.net/tag/ie/" title="ie" rel="tag">ie</a>, <a href="http://jacky.seezone.net/tag/javascript/" title="javascript" rel="tag">javascript</a>, <a href="http://jacky.seezone.net/tag/jquery/" title="jquery" rel="tag">jquery</a><br />
]]></content:encoded>
			<wfw:commentRss>http://jacky.seezone.net/2010/01/13/2760/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>jQuery 1.3 and JS Bin</title>
		<link>http://jacky.seezone.net/2009/01/15/2318/</link>
		<comments>http://jacky.seezone.net/2009/01/15/2318/#comments</comments>
		<pubDate>Thu, 15 Jan 2009 03:34:27 +0000</pubDate>
		<dc:creator>Jacky</dc:creator>
				<category><![CDATA[網絡]]></category>
		<category><![CDATA[電腦]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[jquery]]></category>
		<category><![CDATA[jsbin]]></category>
		<category><![CDATA[programming]]></category>

		<guid isPermaLink="false">http://jacky.seezone.net/?p=2318</guid>
		<description><![CDATA[jQuery 1.3 剛剛出籠，重點是新的 selector engine Sizzle 。效能提昇之餘，更可因此而引進新功能，如 live event delegation ，據說普通的 event delegation 也會更快 (也是因為 selector 快了吧)。而 Sizzle 本身已成為一個獨立專案，讓其他 library 也可以用。 jQuery 的 API Browser 也更新了，在試用之時發覺每一個 code example 都伴隨一個 &#8220;Edit&#8221; 連結，結果是連到 JS Bin 裏可以直接修改和測試 Code，非常方便。 但 JS Bin 不是會每三個月做 house-keeping 嗎？還是特別為 jQuery 做 host 了？結果測看了其 URL，發現原來可以用 http://jsbin.com/?html=[code]&#38;js=[code] 來發送一個完整的例子，當然這會受到 URL 的字數限制了。 Tags: javascript, jquery, jsbin, programming]]></description>
			<content:encoded><![CDATA[	<p><a href="http://blog.jquery.com/2009/01/14/jquery-13-and-the-jquery-foundation/">jQuery 1.3</a> 剛剛出籠，重點是新的 selector engine  <a href="http://sizzlejs.com/">Sizzle</a> 。效能提昇之餘，更可因此而引進新功能，如 <a href="http://docs.jquery.com/Release:jQuery_1.3#Live_Events">live event delegation</a> ，據說普通的 event delegation 也會更快 (也是因為 selector 快了吧)。而 Sizzle 本身已成為一個獨立專案，讓其他 library 也可以用。</p>

	<p>jQuery 的 <a href="http://api.jquery.com/"><span class="caps">API</span> Browser</a> 也更新了，在試用之時發覺每一個 code example 都伴隨一個 &#8220;Edit&#8221; 連結，結果是連到 <a href="http://jsbin.com">JS Bin</a> 裏可以直接修改和測試 Code，非常方便。</p>

	<p>但 JS Bin 不是會每三個月做 house-keeping 嗎？還是特別為 jQuery 做 host 了？結果測看了其 URL，發現原來可以用 <code>http://jsbin.com/?html=[code]&#38;js=[code]</code> 來發送一個完整的例子，當然這會受到 <span class="caps">URL</span> 的字數限制了。</p>
	Tags:  <a href="http://jacky.seezone.net/tag/javascript/" title="javascript" rel="tag">javascript</a>, <a href="http://jacky.seezone.net/tag/jquery/" title="jquery" rel="tag">jquery</a>, <a href="http://jacky.seezone.net/tag/jsbin/" title="jsbin" rel="tag">jsbin</a>, <a href="http://jacky.seezone.net/tag/programming/" title="programming" rel="tag">programming</a><br />
]]></content:encoded>
			<wfw:commentRss>http://jacky.seezone.net/2009/01/15/2318/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>jQuery Plugin &#8211; Stupid Fixed Header for HTML Table</title>
		<link>http://jacky.seezone.net/2008/10/23/2203/</link>
		<comments>http://jacky.seezone.net/2008/10/23/2203/#comments</comments>
		<pubDate>Wed, 22 Oct 2008 17:12:47 +0000</pubDate>
		<dc:creator>Jacky</dc:creator>
				<category><![CDATA[電腦]]></category>
		<category><![CDATA[html]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[jquery]]></category>
		<category><![CDATA[plugins]]></category>
		<category><![CDATA[programming]]></category>

		<guid isPermaLink="false">http://jacky.seezone.net/?p=2203</guid>
		<description><![CDATA[Sometimes you would like to have a nice table to navigate data with the header fixed on top while scrolling. There are many Javascript dataGrid component out there to do this but sometimes you just don&#8217;t need all those features. So I have wrote a jQuery plugin to create a fixed table header on a [...]]]></description>
			<content:encoded><![CDATA[	<p>Sometimes you would like to have a nice table to navigate data with the header fixed on top while scrolling. There are many Javascript dataGrid component out there to do this but sometimes you just don&#8217;t need all those features. So I have wrote a <a href="http://jquery.com">jQuery</a> plugin to create a fixed table header on a normal <span class="caps">HTML</span> table. It&#8217;s called <a href="http://jacky.seezone.net/fixedheader/fixedheader.html">Stupid Fixed Header</a> .</p>

	<p>I called it &#8216;stupid&#8217; because it does so many things to achieve this little effect:</p>

	<ol>
		<li>create extra divs to wrap table up</li>
		<li>cloning current table header</li>
		<li>put the cloned table header on top of the current table</li>
		<li>synchronize width of each cell</li>
		<li>synchronize scroll position when scrolling</li>
	</ol>

	<p>This idea is not original. I have read it elsewhere but cannot find the source again. So I do it in jQuery way.</p>

	<p>The good things:
	<ul>
		<li>supports horizontal scroll</li>
		<li>work across browsers (tested on IE6/7, Firefox3.1, Opera9.5, Chrome0.2, Safari 3.1.2).</li>
	</ul></p>

	<p>The bad things:
	<ul>
		<li>in order to calculate correct cell width in different browsers, I have made certain assumptions in the default implementation: table&#8217;s border-collapse is collapse, cell have borders, etc. Therefore, you may need to make change for the width calculation. This is made possible by supplying <code>adjustWidth</code> to the plugin option. (However, dealing with width in table can be really troublesome. Even current implementation is not pixel perfect.)</li>
		<li>Firefox has a <a href="http://bugzilla.mozilla.org/show_bug.cgi?id=410621">bug of missing border</a> when placing table inside a overflow div. Some specific <span class="caps">CSS</span> rules has been added to the sample page to workaround this.</li>
		<li>It&#8217;s not pixel perfect.</li>
		<li>It may not perform well.</li>
		<li>The header cells will not get resized when the table cells width changes. This can be done by re-calculate the cells width and add custom event to the table for triggering. I&#8217;ll leave it to someone who has interest in doing so&#8230;</li>
	</ul></p>

	<p>To support IE6, I have worked around the z-index bug by hiding &lt;select&gt; component. The reason for not using iframe hack is that it will get flashy when scroll.</p>

	<p>Feel free to check the <a href="http://jacky.seezone.net/fixedheader/fixedheader.html">demo and doc</a> .</p>


	Tags:  <a href="http://jacky.seezone.net/tag/html/" title="html" rel="tag">html</a>, <a href="http://jacky.seezone.net/tag/javascript/" title="javascript" rel="tag">javascript</a>, <a href="http://jacky.seezone.net/tag/jquery/" title="jquery" rel="tag">jquery</a>, <a href="http://jacky.seezone.net/tag/plugins/" title="plugins" rel="tag">plugins</a>, <a href="http://jacky.seezone.net/tag/programming/" title="programming" rel="tag">programming</a><br />
]]></content:encoded>
			<wfw:commentRss>http://jacky.seezone.net/2008/10/23/2203/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Focus first text field using jQuery</title>
		<link>http://jacky.seezone.net/2008/04/14/1979/</link>
		<comments>http://jacky.seezone.net/2008/04/14/1979/#comments</comments>
		<pubDate>Sun, 13 Apr 2008 16:03:54 +0000</pubDate>
		<dc:creator>Jacky</dc:creator>
				<category><![CDATA[電腦]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[jquery]]></category>
		<category><![CDATA[programming]]></category>

		<guid isPermaLink="false">http://jacky.seezone.net/?p=1979</guid>
		<description><![CDATA[When it comes to focus the first text field on a page using jQuery , the usual solution would be: $&#40;&#34;:text:visible:enabled:eq(0)&#34;&#41;.focus&#40;&#41;; This query means to find the first of input text field(s), which is/are visible and enabled. However, this would also extract text field(s) which is/are under a display:none parent(s). So a more complete way [...]]]></description>
			<content:encoded><![CDATA[	<p>When it comes to focus the first text field on a page using <a href="http://www.jquery.com">jQuery</a> , the usual solution would be:</p>


<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;:text:visible:enabled:eq(0)&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #000066;">focus</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>


	<p>This query means to find the first of input text field(s), which is/are visible and enabled. However, this would also extract text field(s) which is/are under a <b>display:none</b> parent(s). So a more complete way would be:</p>


<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;:text:visible:enabled&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">filter</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
     <span style="color: #000066; font-weight: bold;">return</span> $<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">parents</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;:hidden&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">size</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">==</span><span style="color: #CC0000;">0</span><span style="color: #339933;">;</span>
 <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">slice</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">0</span><span style="color: #339933;">,</span><span style="color: #CC0000;">1</span><span style="color: #009900;">&#41;</span>.<span style="color: #000066;">focus</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>


	<p>The <b>filter</b> here would try to determine if the text field is a descendant of a <b>hidden</b> ancestor. The usage of <b>slice(0,1)</b> instead of <b>get(0)</b> is to keep the chainability. A test page is put up <a href="http://www.seezone.net/dev/focusField.html">here</a> .</p>

	<p>Finally, you may wrap it up with a plugin:</p>


<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>$<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
    $.<span style="color: #660066;">fn</span>.<span style="color: #660066;">focusFirstField</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
        $this <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">this</span><span style="color: #339933;">;</span>
        $this.<span style="color: #660066;">find</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;:text:visible:enabled&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">filter</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
            <span style="color: #000066; font-weight: bold;">return</span> $<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">parents</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;:hidden&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">size</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">==</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">slice</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">0</span><span style="color: #339933;">,</span><span style="color: #CC0000;">1</span><span style="color: #009900;">&#41;</span>.<span style="color: #000066;">focus</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #000066; font-weight: bold;">this</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#40;</span>jQuery<span style="color: #009900;">&#41;</span></pre></div></div>


	<p>So that you can do this:</p>


<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#form1,#form2&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">focusFirstField</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">//form1 or form2 may be 'hidden' dynamically</span></pre></div></div>


	<p>I just wonder if there is any &#8216;selector-only&#8217; solution for this?</p>

	<p><b>Update:</b> It turns out that the child&#8217;s visiblity would override parent&#8217;s visibility. So the filter should only check display none:</p>


<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;:text:visible:enabled&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">filter</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
     <span style="color: #000066; font-weight: bold;">return</span> $<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">parents</span>.<span style="color: #660066;">filter</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span> 
         <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">style</span>.<span style="color: #660066;">display</span> <span style="color: #339933;">==</span> <span style="color: #3366CC;">&quot;none&quot;</span><span style="color: #339933;">;</span> <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>
    .<span style="color: #660066;">size</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">==</span><span style="color: #CC0000;">0</span><span style="color: #339933;">;</span>
 <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">slice</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">0</span><span style="color: #339933;">,</span><span style="color: #CC0000;">1</span><span style="color: #009900;">&#41;</span>.<span style="color: #000066;">focus</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>


	<p>I&#8217;ve updated the <a href="http://www.seezone.net/dev/focusField.html">demo page</a> .</p>
	Tags:  <a href="http://jacky.seezone.net/tag/javascript/" title="javascript" rel="tag">javascript</a>, <a href="http://jacky.seezone.net/tag/jquery/" title="jquery" rel="tag">jquery</a>, <a href="http://jacky.seezone.net/tag/programming/" title="programming" rel="tag">programming</a><br />
]]></content:encoded>
			<wfw:commentRss>http://jacky.seezone.net/2008/04/14/1979/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
		<item>
		<title>jQuery validation 的 addMethod</title>
		<link>http://jacky.seezone.net/2008/03/27/1972/</link>
		<comments>http://jacky.seezone.net/2008/03/27/1972/#comments</comments>
		<pubDate>Wed, 26 Mar 2008 18:50:12 +0000</pubDate>
		<dc:creator>Jacky</dc:creator>
				<category><![CDATA[電腦]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[jquery]]></category>
		<category><![CDATA[plugins]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[validation]]></category>

		<guid isPermaLink="false">http://jacky.seezone.net/2008/03/27/1972/</guid>
		<description><![CDATA[jQuery 的 Validation plugins 是很強大的，基本上可以滿足各種需要。剛剛被一個小問題困擾了好一陣子，特此記下。我寫了類似的東西： &#60;div id=&#34;error&#34; style=&#34;display:none&#34;&#62;&#60;ul&#62;&#60;/ul&#62;&#60;/div&#62; &#60;form id=&#34;form1&#34;&#62; &#60;input type=&#34;hidden&#34; name=&#34;fff&#34; id=&#34;fff&#34; value=&#34;&#34; class=&#34;special&#34; /&#62; &#60;/form&#62; $.validator.addMethod&#40;&#34;special&#34;, function&#40;value, element, params&#41;&#123; //validate.... return result; &#125;, &#34;This is a special checking&#34;&#41;; &#160; $&#40;document&#41;.ready&#40;function&#40;&#41;&#123; $&#40;&#34;#form&#34;&#41;.validate&#40;&#123; messages:&#123; fff: &#123;special: &#34;This is a special checking!&#34;&#125; &#125;, errorContainer: &#34;#error&#34;, errorLabelContainer: &#34;#error ul&#34;, wrapper: &#34;li&#34; &#125;&#41;; &#125;&#41;; 結果怎樣也出不到那個自訂的 validation rule，苦苦深研追踪 [...]]]></description>
			<content:encoded><![CDATA[	<p><a href="http://jquery.com">jQuery</a> 的 <a href="http://docs.jquery.com/Plugins/Validation/">Validation</a> plugins 是很強大的，基本上可以滿足各種需要。剛剛被一個小問題困擾了好一陣子，特此記下。我寫了類似的東西：</p>


<div class="wp_syntax"><div class="code"><pre class="html4strict" style="font-family:monospace;"> <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">div</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;error&quot;</span> <span style="color: #000066;">style</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;display:none&quot;</span>&gt;&lt;<span style="color: #000000; font-weight: bold;">ul</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">ul</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">div</span>&gt;</span>
 <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">form</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;form1&quot;</span>&gt;</span>
     <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">input</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;hidden&quot;</span> <span style="color: #000066;">name</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;fff&quot;</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;fff&quot;</span> <span style="color: #000066;">value</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;&quot;</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;special&quot;</span> <span style="color: #66cc66;">/</span>&gt;</span>
 <span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">form</span>&gt;</span></pre></div></div>



<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"> $.<span style="color: #660066;">validator</span>.<span style="color: #660066;">addMethod</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;special&quot;</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>value<span style="color: #339933;">,</span> element<span style="color: #339933;">,</span> params<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
    <span style="color: #006600; font-style: italic;">//validate....</span>
    <span style="color: #000066; font-weight: bold;">return</span> result<span style="color: #339933;">;</span>
 <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;This is a special checking&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
 $<span style="color: #009900;">&#40;</span>document<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">ready</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
     $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#form&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">validate</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>
		messages<span style="color: #339933;">:</span><span style="color: #009900;">&#123;</span> fff<span style="color: #339933;">:</span> <span style="color: #009900;">&#123;</span>special<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;This is a special checking!&quot;</span><span style="color: #009900;">&#125;</span> <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
		errorContainer<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;#error&quot;</span><span style="color: #339933;">,</span>
		errorLabelContainer<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;#error ul&quot;</span><span style="color: #339933;">,</span>
		wrapper<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;li&quot;</span>
     <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
 <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>


	<p>結果怎樣也出不到那個自訂的 validation rule，苦苦深研追踪 source code 後才發現，問題在於 addMethod 裏的 method 參數問題，而正確的寫法是：</p>


<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"> $.<span style="color: #660066;">validator</span>.<span style="color: #660066;">addMethod</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;special&quot;</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
    <span style="color: #006600; font-style: italic;">//validate...</span>
    <span style="color: #000066; font-weight: bold;">return</span> result<span style="color: #339933;">;</span>
 <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;This is a special checking&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>


	<p>那個 method 的參數數量如果大於 3，plugins 會認為你所加的是有參數的 validation，即在 element class 可能是 {special:abc} 之類，所以當看到 input 裏只是 special 就不會偵測到。</p>

	<p>這樣很「正路」吧？但花了不少時間去看才知道。</p>


	Tags:  <a href="http://jacky.seezone.net/tag/javascript/" title="javascript" rel="tag">javascript</a>, <a href="http://jacky.seezone.net/tag/jquery/" title="jquery" rel="tag">jquery</a>, <a href="http://jacky.seezone.net/tag/plugins/" title="plugins" rel="tag">plugins</a>, <a href="http://jacky.seezone.net/tag/programming/" title="programming" rel="tag">programming</a>, <a href="http://jacky.seezone.net/tag/validation/" title="validation" rel="tag">validation</a><br />
]]></content:encoded>
			<wfw:commentRss>http://jacky.seezone.net/2008/03/27/1972/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
	</channel>
</rss>

