Study Area Facebook粉絲團http://www.facebook.com/sataiwan
0 會員 與 1 訪客 正在閱讀本文。
$(document).ready(function(){ $("#add_news_link").click(function(){ $("#mode").attr("value", "add"); });})
$('#myForm :input')
/* 前者 */$('input:radio', myForm)
/* 後者 */$('input[@type=radio]')
$("#news_form :input").each(function(){ switch($(this).attr('type')){ case 'radio': /* 取消所有選取 */ $(this).attr("checked", false); case 'checkbox': /* 取消所有選取 */ $(this).attr("checked", false); break; case 'select-one': /* 把 select 元件都歸到選第一項 */ $(this)[0].selectedIndex = 0; break; case 'text': /* 清空 text 來欄位 */ $(this).attr("value", ""); break; case 'password': /* 清空 password 來欄位 */ $(this).attr("value", ""); case 'hidden': /* * 不清空 hidden,通常保純此欄位 */ case 'textarea': /* 清空 textarea 來欄位 */ $(this).attr("value", ""); break; }});
$('#news_form').clearForm();
if (t == 'text' || t == 'password' || tag == 'textarea') this.value = '';else if (t == 'checkbox' || t == 'radio') this.checked = false;else if (tag == 'select') this.selectedIndex = -1;
$("#news_form").submit( function(e){ if($("#news_name").val() == "") { $("#news_name").focus(); alert("標題沒填寫"); return false; } else if($("#news_desc").val() == "") { $("#news_desc").focus(); alert("內容沒填寫"); return false; } return true; });
/** text 欄位取值*/$("#news_name").val();$("#news_name").arrt("value");/** 取消 checkbox 欄位選取*/$(#news_top).attr("checked", false);$(#news_top).attr("checked", "");/** 判斷 checkbox 是否選取*/if($("#news_top").attr('checked')==undefined)/** 全選 checkbox 欄位,或者反向選取*/$("#clickAll").click(function() { $("input[@name='news_top[]']").each(function() { if($(this).attr("checked")) { $(this).attr("checked", false); } else { $(this).attr("checked", true); } });}); /** checkbox 的 value 值*/$('input[@name="news_top"][@checked]').val();/** 選取 select 的 text 值*/$("select[@name='categories_id']option[@selected]").text();/** 選取 select 的 value 值*/$("select[@name='categories_id']").val();
小補充:1. jQuery 1.3 後不能再用 [@attr=value] 的形式,要改用 [attr=value]2. 最新版的 jQuery Form Plugin 還可以使用 AJAX 檔案上傳,超讚!
Note: In jQuery 1.3 [@attr] style selectors were removed (they were previously deprecated in jQuery 1.2). Simply remove the '@' symbol from your selectors in order to make them work again.