操作DropDownList、CheckBoxList和RadioButton的js
阿新 • • 發佈:2019-01-02
//=============================================select操作 Start============================================= /* 伺服器控制元件dropdownlist再被伺服器解析後,客戶端呈現為select 宣告:如下方法對於自定義的個性化dropdownlist不適用,比如下拉項是div+ul+li的 */ function SelectChangeFn(selectid, _type) { $("#" + selectid).change(function() { switch (_type) { case "dosomething": //... break; //... } }) } //獲取select 選中的 text : function GetSelectByEdText(selectid) { return $("#" + selectid).find("option:selected").text(); } //獲取select 選中的 value : function GetSelectByEdValue(selectid) { return $("#" + selectid).val(); } //獲取select 選中的 索引 : function GetSelectByEdIndex(selectid) { return $("#" + selectid).get(0).selectedIndex; } //設定select 選中的 text : function SetSelectByEdText(selectid, _txt) { var count = $("#" + selectid + " option").length; for (var i = 0; i < count; i++) { if ($("#" + selectid).get(0).options[i].text == _txt) { $("#" + selectid).get(0).options[i].selected = true; break; } } } //設定select 選中的 value : function SetSelectByEdValue(selectid, _val) { //$("#" + selectid).attr("value", _val); //$("#" + selectid).get(0).value = _val; $("#" + selectid).val(_val); } //設定select 選中的 索引 : function SetSelectByEdIndex(selectid, _selectedIndex) { $("#" + selectid).get(0).selectedIndex = _selectedIndex; //index為索引值 } //給select新增一項option(預設為最後,並選中新增項) function AddSelectItemLast(selectid, _val, _txt) {//如果_txt過大可能會把select撐長 var targetS = $("#" + selectid); targetS.append("<option value='" + _val + "'>" + _txt + "</option>"); targetS.val(_val); } //在select前面插入一項option(並選中新增項) function InserSelectItemFrist(selectid, _val, _txt) { var targetS = $("#" + selectid); targetS.prepend("<option value='" + _val + "'>" + _txt + "</option>"); targetS.val(_val); } //刪除text值為_txt的Option function RemoveSelectItemText(selectid, _txt) { var temp; var count = $("#" + selectid + " option").length; for (var i = 0; i < count; i++) { temp = $("#" + selectid).get(0).options[i]; if ($("#" + selectid).get(0).options[i].text == _txt) { $(temp).remove(); break; } } } //刪除value值為_val的Option function RemoveSelectItemValue(selectid, _val) { $("#" + selectid + " option[value='" + _val + "']").remove(); } //刪除索引值為0的Option function RemoveSelectItemIndex(selectid, _itemIndex) { $("#" + selectid + " option:eq(" + _itemIndex + ")").remove(); } //清空 select各項: function EmptySelectItemsAll(selectid) { $("#" + selectid).empty(); } //設定selec t選中的vlue(原生態js): function SetSelectByEdValuOriginal(selectid, _val) { var targetObj=document.getElementById(selectid); var count = targetObj.options.length; for (var i = 0; i < count; i++) { if (targetObj.options[i].value == _val) { targetObj.options[i].selected = true; break; } } } //=============================================select操作 End============================================= //==========================================radiobuttonlist操作 End======================================= //獲取radiobuttonlist 選中的 text : function GetRbtnListByEdText(radiobuttonlistid) { return $("#" + radiobuttonlistid).find("input:checked").next("label").text(); } //獲取radiobuttonlist 選中的 value : function GetRbtnListByEdValue(radiobuttonlistid) { return $("#" + radiobuttonlistid).find("input:checked").attr("value"); } //獲取radiobuttonlist 選中的 索引 : function GetRbtnListByEdIndex(radiobuttonlistid) { var targetObj = $("#" + radiobuttonlistid + " input"); var tempThis = $("#" + radiobuttonlistid).find("input:checked") return targetObj.index(tempThis); } //設定radiobuttonlist 選中的 text : function SetRbtnListByEdText(radiobuttonlistid, _txt) { var tempThis; var targetObj = $("#" + radiobuttonlistid + " label"); targetObj.each(function() { tempThis = $(this); if (tempThis.text() == _txt) { tempThis.prev("input").attr("checked", "checked"); //.attr("disabled", "disabled"); return; } }) } //設定radiobuttonlist 選中的 value : function SetRbtnListByEdValue(radiobuttonlistid, _val) { $("#" + radiobuttonlistid + " input[value='" + _val + "']").attr("checked", "checked"); } //設定radiobuttonlist 選中的 索引 : function SetRbtnListByEdIndex(radiobuttonlistid, _selectedIndex) { var tempThis; var targetObj = $("#" + radiobuttonlistid + " input"); targetObj.each(function() { tempThis = $(this); if (targetObj.index(this) == _selectedIndex) { tempThis.attr("checked", "checked"); return; } }) } //