<?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>I@laoer.com &#187; JavaScript</title>
	<atom:link href="http://i.laoer.com/category/javascript/feed" rel="self" type="application/rss+xml" />
	<link>http://i.laoer.com</link>
	<description>技术、生活、感悟 -- Laoer的博客</description>
	<lastBuildDate>Mon, 15 Feb 2010 08:36:53 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>使用jQuery Autocomplete（自动完成）插件</title>
		<link>http://i.laoer.com/jquery-autocomplete-plugin.html</link>
		<comments>http://i.laoer.com/jquery-autocomplete-plugin.html#comments</comments>
		<pubDate>Fri, 17 Apr 2009 06:53:22 +0000</pubDate>
		<dc:creator>Laoer</dc:creator>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[jQuery]]></category>

		<guid isPermaLink="false">http://i.laoer.com/?p=283</guid>
		<description><![CDATA[jQuery的Autocomplete（自动完成、自动填充）插件有不少，但比较下来我感觉，还是bassistance.de的比较强大，我们就来写一些代码感受一下。
最简单的Autocomplete（自动完成）代码片段

1
2
3
4
5
6
7
8
9
&#60;script type=&#34;text/javascript&#34;&#62;
var websites = &#91;
	&#34;Google&#34;,&#34;NetEase&#34;, &#34;Sohu&#34;, &#34;Sina&#34;, &#34;Sogou&#34;, &#34;Baidu&#34;, &#34;Tencent&#34;, 
	&#34;Taobao&#34;, &#34;Tom&#34;, &#34;Yahoo&#34;, &#34;JavaEye&#34;, &#34;Csdn&#34;, &#34;Alipay&#34;
&#93;;
$&#40;&#41;.ready&#40;function&#40;&#41; &#123;
	$&#40;&#34;#website&#34;&#41;.autocomplete&#40;websites&#41;;	
&#125;&#41;;
&#60;/script&#62;


1
2
3
4
5
6
&#60;p&#62;
&#60;label&#62;Web Site:&#60;/label&#62;
&#60;input type=&#34;text&#34; id=&#34;website&#34; /&#62;
&#60;input type=&#34;button&#34; id=&#34;getvalue&#34; value=&#34;Get Value&#34; /&#62;
&#60;/p&#62;
&#60;div id=&#34;content&#34;&#62;&#60;/div&#62;

我们可以看到效果

这么几行代码就完成了自动完成功能，真实太强了，不过bassistance.de的jQuery Autocomplete插件还有更丰富的功能，它的文档在http://docs.jquery.com/Plugins/Autocomplete，在API Documentation里，我们要仔细的研究一下autocomplete( url or data, [options] )方法。
autocomplete方法有两个参数，第一个用来填写URL地址或是数据，jQuery Autocomplete插件是支持Ajax方式调用数据，所以可以填写调用的地址，另外可以直接填写数据，格式为JavaScript数组，如我们的例子，autocomplete的另外一个参数 [options]是一个可选项，我们在例子中就没有写，但这个参数里面却有很多可配置的参数，我们还是先修改上面的例子

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
$&#40;&#41;.ready&#40;function&#40;&#41; &#123;
	$&#40;&#34;#website&#34;&#41;.autocomplete&#40;websites,&#123;
		minChars: 0,
		max: 5,
		autoFill: true,
		mustMatch: true,
		matchContains: true,
		scrollHeight: 220,
		formatItem: function&#40;data, i, total&#41; &#123;
			return &#34;&#60;I&#62;&#34;+data&#91;0&#93;+&#34;&#60;/I&#62;&#34;;
		&#125;,
		formatMatch: function&#40;data, i, total&#41; &#123;
			return data&#91;0&#93;;
		&#125;,
		formatResult: function&#40;data&#41; &#123;
			return [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://jquery.com/" target="_blank">jQuery</a>的Autocomplete（自动完成、自动填充）插件有不少，但比较下来我感觉，还是<a href="http://bassistance.de/jquery-plugins/jquery-plugin-autocomplete/" target="_blank">bassistance.de</a>的比较强大，我们就来写一些代码感受一下。</p>
<p>最简单的Autocomplete（自动完成）代码片段</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #339933;">&lt;</span>script type<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;text/javascript&quot;</span><span style="color: #339933;">&gt;</span>
<span style="color: #003366; font-weight: bold;">var</span> websites <span style="color: #339933;">=</span> <span style="color: #009900;">&#91;</span>
	<span style="color: #3366CC;">&quot;Google&quot;</span><span style="color: #339933;">,</span><span style="color: #3366CC;">&quot;NetEase&quot;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;Sohu&quot;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;Sina&quot;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;Sogou&quot;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;Baidu&quot;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;Tencent&quot;</span><span style="color: #339933;">,</span> 
	<span style="color: #3366CC;">&quot;Taobao&quot;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;Tom&quot;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;Yahoo&quot;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;JavaEye&quot;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;Csdn&quot;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;Alipay&quot;</span>
<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
$<span style="color: #009900;">&#40;</span><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;#website&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">autocomplete</span><span style="color: #009900;">&#40;</span>websites<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>
<span style="color: #339933;">&lt;/</span>script<span style="color: #339933;">&gt;</span></pre></td></tr></table></div>


<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
</pre></td><td class="code"><pre class="html" style="font-family:monospace;">&lt;p&gt;
&lt;label&gt;Web Site:&lt;/label&gt;
&lt;input type=&quot;text&quot; id=&quot;website&quot; /&gt;
&lt;input type=&quot;button&quot; id=&quot;getvalue&quot; value=&quot;Get Value&quot; /&gt;
&lt;/p&gt;
&lt;div id=&quot;content&quot;&gt;&lt;/div&gt;</pre></td></tr></table></div>

<p>我们可以看到效果</p>
<p><img class="alignnone size-full wp-image-284" title="jQuery Autocomplete Plugin" src="http://i.laoer.com/wp-content/uploads/2009/04/jquery-autocomplete-1.jpg" alt="jQuery Autocomplete Plugin" width="314" height="109" /></p>
<p>这么几行代码就完成了自动完成功能，真实太强了，不过bassistance.de的jQuery Autocomplete插件还有更丰富的功能，它的文档在<a href="http://docs.jquery.com/Plugins/Autocomplete" target="_blank">http://docs.jquery.com/Plugins/Autocomplete</a>，在API Documentation里，我们要仔细的研究一下autocomplete( url or data, [options] )方法。</p>
<p>autocomplete方法有两个参数，第一个用来填写URL地址或是数据，jQuery Autocomplete插件是支持Ajax方式调用数据，所以可以填写调用的地址，另外可以直接填写数据，格式为JavaScript数组，如我们的例子，autocomplete的另外一个参数 [options]是一个可选项，我们在例子中就没有写，但这个参数里面却有很多可配置的参数，我们还是先修改上面的例子</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;">$<span style="color: #009900;">&#40;</span><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;#website&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">autocomplete</span><span style="color: #009900;">&#40;</span>websites<span style="color: #339933;">,</span><span style="color: #009900;">&#123;</span>
		minChars<span style="color: #339933;">:</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">,</span>
		max<span style="color: #339933;">:</span> <span style="color: #CC0000;">5</span><span style="color: #339933;">,</span>
		autoFill<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">true</span><span style="color: #339933;">,</span>
		mustMatch<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">true</span><span style="color: #339933;">,</span>
		matchContains<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">true</span><span style="color: #339933;">,</span>
		scrollHeight<span style="color: #339933;">:</span> <span style="color: #CC0000;">220</span><span style="color: #339933;">,</span>
		formatItem<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>data<span style="color: #339933;">,</span> i<span style="color: #339933;">,</span> total<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			<span style="color: #000066; font-weight: bold;">return</span> <span style="color: #3366CC;">&quot;&lt;I&gt;&quot;</span><span style="color: #339933;">+</span>data<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">+</span><span style="color: #3366CC;">&quot;&lt;/I&gt;&quot;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
		formatMatch<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>data<span style="color: #339933;">,</span> i<span style="color: #339933;">,</span> total<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			<span style="color: #000066; font-weight: bold;">return</span> data<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
		formatResult<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>data<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			<span style="color: #000066; font-weight: bold;">return</span> data<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#93;</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: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>在options项我们增加了好几个参数</p>
<p>minChars表示在自动完成激活之前填入的最小字符，这里我们设置为0，在我们双击文本框，不输入字符的时候，就会把数据显示出来，效果如下</p>
<p><img class="alignnone size-full wp-image-286" title="jquery-autocomplete-21" src="http://i.laoer.com/wp-content/uploads/2009/04/jquery-autocomplete-21.jpg" alt="jquery-autocomplete-21" width="323" height="139" /></p>
<p>max表示列表里的条目数，我们设置了5，所以显示5条，也如上图</p>
<p>autoFill表示自动填充，就是在文本框中自动填充符合条件的项目，看下图，在我们输入“g”的时候，文本框里填充了“google”</p>
<p><img class="alignnone size-full wp-image-287" title="jQuery Autocomplete Plugin" src="http://i.laoer.com/wp-content/uploads/2009/04/jquery-autocomplete-3.jpg" alt="jQuery Autocomplete Plugin" width="317" height="79" /></p>
<p>mustMatch表示必须匹配条目，也就是在文本框里输入的内容，必须是data参数里的数据，如果不匹配，文本框就被清空</p>
<p>matchContains表示包含匹配，就是data参数里的数据，是否只要包含文本框里的数据就显示，比如在上面的图中，我们输入了“g”，由于“Sogou”中也包含一个“g”，所以会显示出来，如果将matchContains设为fasle，则“Sogou”就不会显示</p>
<p>scrollHeight不用多解释，看文档就知道。</p>
<p>后面3个参数formatItem、formatMatch、formatResult非常有用，formatItem作用在于可以格式化列表中的条目，比如我们加了“I”，让列表里的字显示出了斜体，formatMatch是配合formatItem使用，作用在于，由于使用了formatItem，所以条目中的内容有所改变，而我们要匹配的是原始的数据，所以用formatMatch做一个调整，使之匹配原始数据，formatResult是定义最终返回的数据，比如我们还是要返回原始数据，而不是formatItem过的数据。</p>
<p>[options]里还有很多有用的参数，大家可以看它的文档。</p>
<p>jQuery Autocomplete插件里还有两个重要的方法，一个是result( handler )，一个是search( )，比如用户在选定了某个条目时需要触发一些别的方法时，着两个方法就可以起作用了，我们再修改以上的例子</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;">$<span style="color: #009900;">&#40;</span><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>
&nbsp;
	<span style="color: #003366; font-weight: bold;">function</span> findValueCallback<span style="color: #009900;">&#40;</span>event<span style="color: #339933;">,</span> data<span style="color: #339933;">,</span> formatted<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#content&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">html</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;&lt;strong&gt;&quot;</span><span style="color: #339933;">+</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span>data <span style="color: #339933;">?</span> <span style="color: #3366CC;">&quot;No match!&quot;</span> <span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;Selected: &quot;</span> <span style="color: #339933;">+</span> formatted<span style="color: #009900;">&#41;</span><span style="color: #339933;">+</span><span style="color: #3366CC;">&quot;&lt;/strong&gt;&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#website&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">autocomplete</span><span style="color: #009900;">&#40;</span>websites<span style="color: #339933;">,</span><span style="color: #009900;">&#123;</span>
		minChars<span style="color: #339933;">:</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">,</span>
		max<span style="color: #339933;">:</span> <span style="color: #CC0000;">5</span><span style="color: #339933;">,</span>
		autoFill<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">true</span><span style="color: #339933;">,</span>
		mustMatch<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">true</span><span style="color: #339933;">,</span>
		matchContains<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">true</span><span style="color: #339933;">,</span>
		scrollHeight<span style="color: #339933;">:</span> <span style="color: #CC0000;">220</span><span style="color: #339933;">,</span>
		formatItem<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>data<span style="color: #339933;">,</span> i<span style="color: #339933;">,</span> total<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			<span style="color: #000066; font-weight: bold;">return</span> <span style="color: #3366CC;">&quot;&lt;I&gt;&quot;</span><span style="color: #339933;">+</span>data<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">+</span><span style="color: #3366CC;">&quot;&lt;/I&gt;&quot;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
		formatMatch<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>data<span style="color: #339933;">,</span> i<span style="color: #339933;">,</span> total<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			<span style="color: #000066; font-weight: bold;">return</span> data<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
		formatResult<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>data<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			<span style="color: #000066; font-weight: bold;">return</span> data<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#93;</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: #339933;">;</span>
	$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#website&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">result</span><span style="color: #009900;">&#40;</span>findValueCallback<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#getvalue&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">click</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;#website&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">search</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</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></td></tr></table></div>

<p>看看是什么效果，会在content div的地方显示出我们选择的内容。</p>
<p>jQuery Autocomplete插件所带的例子还是很好的，大家可以仔细研究一下它的例子，更加灵活的运用jQuery Autocomplete插件。</p>
]]></content:encoded>
			<wfw:commentRss>http://i.laoer.com/jquery-autocomplete-plugin.html/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>用jQuery Form Plugin实现Ajax无刷新的文件上传</title>
		<link>http://i.laoer.com/jquery-form-plugin-ajax-file-upload.html</link>
		<comments>http://i.laoer.com/jquery-form-plugin-ajax-file-upload.html#comments</comments>
		<pubDate>Thu, 26 Feb 2009 08:37:13 +0000</pubDate>
		<dc:creator>Laoer</dc:creator>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[Ajax]]></category>
		<category><![CDATA[jQuery]]></category>

		<guid isPermaLink="false">http://i.laoer.com/?p=120</guid>
		<description><![CDATA[在我以前的意识里，觉得用JavaScript或Ajax提交文件表单似乎是一件麻烦的事情，也没有太仔细的研究，后来看jQuery的时候，发现了jQuery Form Plugin这个插件，它方便的实现了Ajax方式的表单提交，例子里也包括了文件表单的提交，那我也来试一下吧，还是用Kohana（最近专注于这个，而且PHP开发要快多了，要是用Struts，要费好多功夫）。
要Kohana支持上传，先要配置一些upload的参数，把system\config\upload.php拷贝到application/config下，里面的参数做好设置，具体参考Kohana的文档吧，之后创建一个Controller

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
class Myupload_Controller extends Top_Controller &#123;
&#160;
	public function index&#40;&#41; &#123;
		$view = new View &#40;&#34;upload&#34;&#41;;		
		$view-&#62;msg = &#34;Upload File&#34;;
&#160;
		$view-&#62;render &#40; TRUE &#41;;
	&#125;
&#160;
	public function upload&#40;&#41; &#123;		
		$filename = upload::save&#40;'myfile'&#41;;
		echo $filename;
&#160;
		exit &#40;&#41;;
	&#125;
&#160;
&#125;

Top_Controller为我自己创建的类，也继承自Kohana的Controller，之后创建upload.php的view文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
&#60;html&#62;
&#60;head&#62;
&#60;title&#62;&#60;?php echo $msg;?&#62;&#60;/title&#62;
&#60;script type=&#34;text/javascript&#34; src=&#34;&#60;?=url::base()?&#62;js/jquery-1.3.2.min.js&#34;&#62;&#60;/script&#62;
&#60;script type=&#34;text/javascript&#34; src=&#34;&#60;?=url::base()?&#62;js/jquery.form.js&#34;&#62;&#60;/script&#62;
&#60;script type=&#34;text/javascript&#34;&#62; 
        // wait for the DOM to be loaded 
     [...]]]></description>
			<content:encoded><![CDATA[<p>在我以前的意识里，觉得用JavaScript或Ajax提交文件表单似乎是一件麻烦的事情，也没有太仔细的研究，后来看<a href="http://jquery.com/" target="_blank">jQuery</a>的时候，发现了<a href="http://malsup.com/jquery/form/" target="_blank">jQuery Form Plugin</a>这个插件，它方便的实现了Ajax方式的表单提交，例子里也包括了文件表单的提交，那我也来试一下吧，还是用Kohana（最近专注于这个，而且PHP开发要快多了，要是用Struts，要费好多功夫）。</p>
<p>要Kohana支持上传，先要配置一些upload的参数，把system\config\upload.php拷贝到application/config下，里面的参数做好设置，具体参考Kohana的文档吧，之后创建一个Controller</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">class</span> Myupload_Controller <span style="color: #000000; font-weight: bold;">extends</span> Top_Controller <span style="color: #009900;">&#123;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> index<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000088;">$view</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> View <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;upload&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>		
		<span style="color: #000088;">$view</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">msg</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;Upload File&quot;</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #000088;">$view</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">render</span> <span style="color: #009900;">&#40;</span> <span style="color: #009900; font-weight: bold;">TRUE</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> upload<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>		
		<span style="color: #000088;">$filename</span> <span style="color: #339933;">=</span> upload<span style="color: #339933;">::</span><span style="color: #004000;">save</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'myfile'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$filename</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #990000;">exit</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>Top_Controller为我自己创建的类，也继承自Kohana的Controller，之后创建upload.php的view文件</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
</pre></td><td class="code"><pre class="html4strict" style="font-family:monospace;"><span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">html</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">head</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">title</span>&gt;&lt;?php echo $msg;?&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">title</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">script</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;text/javascript&quot;</span> <span style="color: #000066;">src</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;&lt;?=url::base()?&gt;</span></span>js/jquery-1.3.2.min.js&quot;&gt;<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">script</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">script</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;text/javascript&quot;</span> <span style="color: #000066;">src</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;&lt;?=url::base()?&gt;</span></span>js/jquery.form.js&quot;&gt;<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">script</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">script</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;text/javascript&quot;</span>&gt;</span> 
        // wait for the DOM to be loaded 
        $(document).ready(function() { 
        	var options = { 
        		target:        '#fileinfo'   // target element(s) to be updated with server response         
        	};
&nbsp;
        	$('#myForm').submit(function() { 
        		$(this).ajaxSubmit(options);
        		return false; 
        	});
&nbsp;
        }); 
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">script</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">head</span>&gt;</span>
&nbsp;
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">body</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">h2</span>&gt;&lt;?php echo $msg;?&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">h2</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;myForm&quot;</span> <span style="color: #000066;">action</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;&lt;?=url::base()?&gt;</span></span>myupload/upload&quot; method=&quot;post&quot;
	enctype=&quot;multipart/form-data&quot;&gt;
	<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">input</span> <span style="color: #000066;">name</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;myfile&quot;</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;file&quot;</span> <span style="color: #66cc66;">/</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;submit&quot;</span> <span style="color: #000066;">value</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;Submit Comment&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>
<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;fileinfo&quot;</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: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">body</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">html</span>&gt;</span></pre></td></tr></table></div>

<p>在这里我们用jQuery Form Plugin来提交表单，之后将信显示在“fileinfo”这个div里，去试试效果吧。</p>
<p>在<a href="http://malsup.com/jquery/form/" target="_blank">http://malsup.com/jquery/form/</a>里有详细的API和例子，同时<a href="http://malsup.com/jquery/" target="_blank">http://malsup.com/jquery/</a>里也有其他的一些不错的jQuery的插件。</p>
]]></content:encoded>
			<wfw:commentRss>http://i.laoer.com/jquery-form-plugin-ajax-file-upload.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>从JavaEye上找的Google翻译JavaScript脚本</title>
		<link>http://i.laoer.com/google-translate-javascript-from-javaeye.html</link>
		<comments>http://i.laoer.com/google-translate-javascript-from-javaeye.html#comments</comments>
		<pubDate>Mon, 23 Feb 2009 07:10:49 +0000</pubDate>
		<dc:creator>Laoer</dc:creator>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[Google]]></category>

		<guid isPermaLink="false">http://i.laoer.com/?p=99</guid>
		<description><![CDATA[在Blog或新闻发布的时候，我们希望URL是一段有意义的字串，例如我Blog里的《Linux下切分Tomcat的catalina.out日志文件》文章链接是http://i.laoer.com/2009/02/18/rotating-catalina-out-in-tomcat-using-cronolog/，这样有利于SEO（搜索引擎优化），对于我们母语为中文的小虾，有时候要翻译标题还是有点头疼的，还好有了Google翻译，让翻译的事情变得简单了一点，但对于开发者，怎么使用户也方便的翻译，特别是在写Blog的时候就能把标题翻译好，还要动点脑筋，我看到JavaEye里发布新闻的地方有个“让Google帮助生成永久链接”的功能挺好的，看了一下他们的源码，得到以下这段，顺别也解释一下

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
&#60;script type=&#34;text/javascript&#34; src=&#34;http://www.google.com/jsapi&#34;&#62;&#60;/script&#62; //载入Google的jsapi
&#160;
&#60;script type=&#34;text/javascript&#34;&#62;  
&#160;
  function slugify&#40;str&#41; &#123; //这个方法应该是将字符转为小写，并把一些特殊字符转为&#34;-&#34;
    return str.toLowerCase&#40;&#41;.replace&#40;/[^a-z0-9-_]+/g, '-'&#41;.replace&#40;/^-&#124;-$/g, ''&#41;;
  &#125;
&#160;
  google.load&#40;&#34;language&#34;, &#34;1&#34;&#41;; //载入Google的language Ajax库
&#160;
  function translate_title&#40;&#41; &#123; //这里就是翻译文章标题了，里面的方法需要jQuery支持，之前要引入jQuery的包，这里就省略了
    if&#40;$F&#40;&#34;news_title&#34;&#41;.blank&#40;&#41;&#41; &#123;
      alert&#40;&#34;请先填写标题&#34;&#41;;
      return;
    &#125;
    [...]]]></description>
			<content:encoded><![CDATA[<p>在Blog或新闻发布的时候，我们希望URL是一段有意义的字串，例如我Blog里的《Linux下切分Tomcat的catalina.out日志文件》文章链接是http://i.laoer.com/2009/02/18/rotating-catalina-out-in-tomcat-using-cronolog/，这样有利于SEO（搜索引擎优化），对于我们母语为中文的小虾，有时候要翻译标题还是有点头疼的，还好有了<a href="http://translate.google.cn/" target="_blank">Google翻译</a>，让翻译的事情变得简单了一点，但对于开发者，怎么使用户也方便的翻译，特别是在写Blog的时候就能把标题翻译好，还要动点脑筋，我看到<a href="http://www.javaeye.com" target="_blank">JavaEye</a>里发布新闻的地方有个“让Google帮助生成永久链接”的功能挺好的，看了一下他们的源码，得到以下这段，顺别也解释一下</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;">&lt;script type=&quot;text/javascript&quot; src=&quot;http://www.google.com/jsapi&quot;&gt;&lt;/script&gt; //载入Google的jsapi
&nbsp;
<span style="color: #339933;">&lt;</span>script type<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;text/javascript&quot;</span><span style="color: #339933;">&gt;</span>  
&nbsp;
  <span style="color: #003366; font-weight: bold;">function</span> slugify<span style="color: #009900;">&#40;</span>str<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #006600; font-style: italic;">//这个方法应该是将字符转为小写，并把一些特殊字符转为&quot;-&quot;</span>
    <span style="color: #000066; font-weight: bold;">return</span> str.<span style="color: #660066;">toLowerCase</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">replace</span><span style="color: #009900;">&#40;</span><span style="color: #009966; font-style: italic;">/[^a-z0-9-_]+/g</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'-'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">replace</span><span style="color: #009900;">&#40;</span><span style="color: #009966; font-style: italic;">/^-|-$/g</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">''</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
  google.<span style="color: #660066;">load</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;language&quot;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;1&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">//载入Google的language Ajax库</span>
&nbsp;
  <span style="color: #003366; font-weight: bold;">function</span> translate_title<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;">//这里就是翻译文章标题了，里面的方法需要jQuery支持，之前要引入jQuery的包，这里就省略了</span>
    <span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>$F<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;news_title&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">blank</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      <span style="color: #000066;">alert</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;请先填写标题&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #000066; font-weight: bold;">return</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
    $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;news_slug_url&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">value</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;正在翻译中...&quot;</span><span style="color: #339933;">;</span>
    google.<span style="color: #660066;">language</span>.<span style="color: #660066;">translate</span><span style="color: #009900;">&#40;</span>$F<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;news_title&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;zh&quot;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;en&quot;</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>result<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span>result.<span style="color: #660066;">error</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;news_slug_url&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">value</span> <span style="color: #339933;">=</span> slugify<span style="color: #009900;">&#40;</span>result.<span style="color: #660066;">translation</span><span style="color: #009900;">&#41;</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: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>  
<span style="color: #339933;">&lt;/</span>script<span style="color: #339933;">&gt;</span></pre></td></tr></table></div>

<p>这里有个地方要说一下，<a href="http://code.google.com/apis/ajaxlibs/" target="_blank">Google的Ajax库API</a>还是挺好的，可以load几种Ajax的库，另外<a href="http://code.google.com/intl/zh-CN/webtoolkit/" target="_blank">Google的Web 工具</a>包也可以看看。</p>
]]></content:encoded>
			<wfw:commentRss>http://i.laoer.com/google-translate-javascript-from-javaeye.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
