http://docs.jquery.com/Ajax/serialize
http://docs.jquery.com/Selectors/checkbox
2008年8月29日 星期五
2008年8月12日 星期二
jQuery tutorial, find and filter
http://www.learningjquery.com/2006/11/how-to-get-anything-you-want-part-1
http://www.learningjquery.com/2006/12/how-to-get-anything-you-want-part-2
http://www.learningjquery.com/2006/12/how-to-get-anything-you-want-part-2
filter will select a certain subset (zero or more) of the already
selected elements.find will select a set of (zero or more) elements that are descendants
of the already selected elements.Here is an example:
$('div').filter('.elephants'); // <-- selects the second div, because it has class="elephants"$('div').find('.elephants'); // <-- selects the first paragraph, because it has class="elephants"
* Note that these two examples are very simple and would probably be
better written as ...$('div.elephants');
... and ...
$('div .elephants');$('li + li > a[@href$=pdf]')gets all links ending in
“pdf” that are children of any list item that has another list item as
its previous sibling. It won’t get the first list item’s silly.pdf
because that list item has no other list items before it.$('span:hidden')gets any span element that is hidden.$('li:even')gets all odd-numbered list items (because, in javascript, numbering starts with 0, not 1).$('li:lt(3)')gets the first 3 list items. “lt” stands for “less than,” and it starts counting at 0, not 1.$('li:not(.goofy)')gets list items 1, 2, and 4, because they’re not “goofy.”$('p a[@href*=#]')gets any links that are inside a paragraph and have an “href” attributestarting with “#” —containing “#” anywhere.in other words, same-page links. There are problems with trying to identify same-page links this way. I’ll write about this in an upcoming entry. Note the space between the “p” and the “a”; that means that the “a” is a descendant of the “p.”
2008年5月1日 星期四
jquery, select, change, inject
http://docs.jquery.com/Events/change#examples
Problem
想把select 到的item value 加到text field裡
<%= text_field_tag :email %>
<%= select :contact, :user, @me.contacts.inject([]){|a, user| a << [user.nickname, user.nickname] }, :id => 'select_user', :prompt => '通訊錄聯絡人' %>
Solution
http://jquery.com/api/
http://visualjquery.com/1.1.2.html
$('#contact_user option:selected') 取到的是jQuery object 他是沒有value 的
要用val() 去取
$('#contact_user option:selected').get(0) 取到的才是該 element 才有 value attribute
所以也可以寫成$('#contact_user option:selected').get(0).value = "ooxx" 當然比較麻煩
append 是加在element 後, 不是value
Problem
想把select 到的item value 加到text field裡
<%= text_field_tag :email %>
<%= select :contact, :user, @me.contacts.inject([]){|a, user| a << [user.nickname, user.nickname] }, :id => 'select_user', :prompt => '通訊錄聯絡人' %>
Solution
http://jquery.com/api/
http://visualjquery.com/1.1.2.html
<script type="text/javascript">
$(document).ready(function(){
$('#contact_user').change(function(){
str = $('#contact_user option:selected').val();
contact_str = $('#email').val();
$('#email').val(contact_str+','+str);
});
</script>
$('#contact_user option:selected') 取到的是jQuery object 他是沒有value 的
要用val() 去取
$('#contact_user option:selected').get(0) 取到的才是該 element 才有 value attribute
所以也可以寫成$('#contact_user option:selected').get(0).value = "ooxx" 當然比較麻煩
append 是加在element 後, 不是value
find, sql in ; select detect partition concat gsub split
http://www.1keydata.com/sql/sqlin.html
http://www.rubyinside.com/clever-find-conditions-in-rails-without-sql-56.html
list = [1,2]
User.find(:all, :conditions => {:id => list, :staus => 'health'})
User.find(:all, :conditions => ["follows_counter =0 and id In (?)",list])
SELECT * FROM `users` WHERE (`users`.`id` IN (1,2))
http://www.ruby-doc.org/core/classes/Enumerable.html#M003154
#把參數處理成 array
emails = params[:email].gsub(/\s/, "").gsub(/;/, ",").split(/,/)
in_email_users = User.find(:all, :conditions => {:nickname => emails })
in_email = in_email_users.map(&:nickname)
#就像是兩個回圈下去跑
out_emails = emails.select { |e| !in_email.detect{|i| i == e } }
#email 的驗證 regular expression
out_emails, wrong_emails = out_emails.partition { |e|
e =~ /^([_a-z0-9-]+)(\.[_a-z0-9-]+)*@([a-z0-9-]+)(\.[a-z0-9-]+)*(\.[a-z]{2,4})$/
}
out_emails.concat(in_email)
>> (1..20).select {|i| i % 5 == 0 }
=> [5, 10, 15, 20]
>> (1..20).detect {|i| i % 5 == 0 }
=> 5
http://www.rubyinside.com/clever-find-conditions-in-rails-without-sql-56.html
list = [1,2]
User.find(:all, :conditions => {:id => list, :staus => 'health'})
User.find(:all, :conditions => ["follows_counter =0 and id In (?)",list])
SELECT * FROM `users` WHERE (`users`.`id` IN (1,2))
http://www.ruby-doc.org/core/classes/Enumerable.html#M003154
#把參數處理成 array
emails = params[:email].gsub(/\s/, "").gsub(/;/, ",").split(/,/)
in_email_users = User.find(:all, :conditions => {:nickname => emails })
in_email = in_email_users.map(&:nickname)
#就像是兩個回圈下去跑
out_emails = emails.select { |e| !in_email.detect{|i| i == e } }
#email 的驗證 regular expression
out_emails, wrong_emails = out_emails.partition { |e|
e =~ /^([_a-z0-9-]+)(\.[_a-z0-9-]+)*@([a-z0-9-]+)(\.[a-z0-9-]+)*(\.[a-z]{2,4})$/
}
out_emails.concat(in_email)
>> (1..20).select {|i| i % 5 == 0 }
=> [5, 10, 15, 20]
>> (1..20).detect {|i| i % 5 == 0 }
=> 5
訂閱:
文章 (Atom)