当前位置:编程学习 > C#/ASP.NET >>

他们说过年要发一些代码,不然过的都是衰年。JS:分享一些操作DropDownList、CheckBoxList和RadioButton的js

大过年的,各位coder们应该分享点东西出来嘛,这么大了没人发红包了,发点代码当红包
用c#操作DropDownList、CheckBoxList和RadioButton都很好处理的,就补贴了,这里贴一些用js来操作DropDownList、RadioButton(CheckBoxList和RadioButton差不多)(事实上它们经过服务器解析后都成了html了,你懂的)。
代码比没技术,大手可以无视了哈,重在参与过年发代码的活动和新手学习循序渐进,欢迎大家补充,也可以贴一些自己心得的代码,为他人造福哈
记得把jq库先引入哦:http://ajax.googleapis.com/ajax/libs/jquery/1.9.0/jquery.min.js

//=============================================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;
        }
    })
}

//==========================================radiobuttonlist操作 End=========================================
radiobutton javascript dropdownlist --------------------编程问答-------------------- checkboxlist和roadiobuttonlist差不多,应该可以照着做的 --------------------编程问答-------------------- 这个必须顶和收藏。
Happy year of the snake --------------------编程问答-------------------- 谢谢分享,学习收藏了 --------------------编程问答-------------------- 分享下js操作url的代码,求2013年好运:
//=============================================URL操作 Start=============================================
var StringToAscii = function(str) { return str.charCodeAt(0).toString(16); }
var AsciiToString = function(asccode) { return String.fromCharCode(asccode); }
/* Url编码 */
function SetUrlEncode(unzipStr) {
    var zipstr = "";
    var strSpecial = "!\"#$%&'()*+,/:;<=>?[]^`{|}~%";
    var tt = "";
    for (var i = 0; i < unzipStr.length; i++) {
        var chr = unzipStr.charAt(i);
        var c = StringToAscii(chr);
        tt += chr + ":" + c + "n";
        if (parseInt("0x" + c) > 0x7f) {
            zipstr += encodeURI(unzipStr.substr(i, 1));
        } else {
            if (chr == " ")
                zipstr += "+";
            else if (strSpecial.indexOf(chr) != -1)
                zipstr += "%" + c.toString(16);
            else
                zipstr += chr;
        }
    }
    return zipstr;
}
/* Url解码 */
function SetUrlDecode(zipStr) {
    var uzipStr = "";
    for (var i = 0; i < zipStr.length; i++) {
        var chr = zipStr.charAt(i);
        if (chr == "+") {
            uzipStr += " ";
        } else if (chr == "%") {
            var asc = zipStr.substring(i + 1, i + 3);
            if (parseInt("0x" + asc) > 0x7f) {
                uzipStr += decodeURI("%" + asc.toString() + zipStr.substring(i + 3, i + 9).toString());
                i += 8;
            } else {
                uzipStr += AsciiToString(parseInt("0x" + asc));
                i += 2;
            }
        } else {
            uzipStr += chr;
        }
    }
    return uzipStr;
}

//在当前url地址后面添加时间戳,构造新增的url地址
function SetUrlRefresh(url) {
    if (url.indexOf("?") > 0)
        return url + "&t=" + (new Date().getTime());
    else
        return url + "?t=" + (new Date().getTime());
}

/// <summary>
/// 构造一个地址栏参数对象,把获取的参数存入此对象中,类似数组
/// </summary>
///<remarks>
///如:http://localhost:4838/Joan_Web/test.aspx?fds=153&fdaf=1mlk
///则会得到:
///         fds="153"
///         fdaf="1mlk"
///         getallparameter="fds=153&fdaf=1mlk"
///</remarks>
function UrlSearch() { //重复时只取最后一个
    var name, value;
    var str = window.location.href; //取得整个地址栏
    var num = str.indexOf("?")
    str = str.substr(num + 1); //取得所有参数
    var arr = str.split("&"); //各个参数放到数组里
    for (var i = 0; i < arr.length; i++) {
        num = arr[i].indexOf("=");
        if (num > 0) {
            name = arr[i].substring(0, num);
            value = arr[i].substr(num + 1);
            this[name] = value;
        }
    }
    this["getallparameter"] = str;
}

var RQ = new UrlSearch(); //实例化

//获取url参数名值对,如:&str=123
function GetUrlParamAndValue(parameter) {
    eval("var temp=RQ." + parameter); //用eval去执行字符串js
    if ((typeof (temp) == "string") && (typeof (temp) != null)) {
        return "&" + parameter + "=" + temp.replace(/(^\s*)|(\s*$)/g, "");
    }
    else
        return "";
}

//获得url地址栏对应参数名的参数值
function GetUrlQueryValueByName(pName) {
    var reg = new RegExp("(^|&)" + pName + "=([^&]*)(&|$)", "i");
    var r = window.location.search.substr(1).match(reg);
    if (r != null)
        return unescape(r[2]);
    return "";
}
/*
function GetUrlParameter(pname) {
    var query = location.search.substring(1);
    var qq = "";
    params = query.split("&");
    if (params.length > 0) {
        for (var n in params) {
            var pairs = params[n].split("=");
            if (pairs[0] == pname) {
                qq = pairs[1];
                break;
            }
        }
    }
    return qq;
}
*/

//获取锚点
function GetUrlAnchor() {
    var str = window.location.href;
    var num = str.indexOf("#")
    str = str.substr(num + 1);
    return str;
}

/*获取当前页页码*/
function GetThisPageIndex() {
    var r = /^[1-9][0-9]*$/;
    if (GetUrlQueryValueByName('pageindex') == '') return 1;
    if (r.test(GetUrlQueryValueByName('pageindex')))
        return parseInt(GetUrlQueryValueByName('pageindex'));
    else
        return 1;
}
//获取URL主机头
function GetUrlHost() {
    if (document.URL.substr(0, 7) == 'http://')
        return 'http://' + window.location.host;
    else
        return 'https://' + window.location.host;
}
//=============================================URL操作 End=============================================
补充:.NET技术 ,  ASP.NET
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,