基本作用頁面中的 radio、checkbox、select,從數(shù)據(jù)庫獲取數(shù)據(jù)庫后給這個方法傳值,讓相應(yīng)的值自動 selected,最常用的就時select下拉框了;需求大神幫忙優(yōu)化如下代碼(如有優(yōu)化空間);(可選)還有我的部分頁面還有一些 <li> 組成的導(dǎo)航,如果也能通過傳值讓<li> addclass('active')的話最好了,一個方法搞定。/* ---------------- * - 表單組件值自動選擇 * ---------------- */function setValue(name, value){ var first = name.substr(0,1), input, i = 0, val; if(value === "") return; if("#" === first || "." === first){ input = $(name); } else { input = $("[name='" + name + "']"); } if(input.eq(0).is(":radio")) { //單選按鈕 input.filter("[value='" + value + "']").each(function(){this.checked = true}); } else if(input.eq(0).is(":checkbox")) { //復(fù)選框 if(!$.isArray(value)){ val = new Array(); val[0] = value; } else { val = value; } for(i = 0, len = val.length; i < len; i++){ input.filter("[value='" + val[i] + "']").each(function(){this.checked = true}); } } else { //其他表單選項(xiàng)直接設(shè)置值 input.val(value); }}
一段 Jquery 代碼,求大神優(yōu)化補(bǔ)充。
狐的傳說
2018-09-14 16:10:45