[jQuery]清除表單欄位值

  • 19089
  • 0
  • 2013-11-20

摘要:[jQuery]清除表單欄位值

總整理:

清空下拉式選單:


//清除DropDownList,這是用selectedIndex的方式去清除
    $('#' + strTableID + ' select').each(function () {
        this.selectedIndex = 0;
    });

//或是用option而且取index為零的方式,去清除
//把下拉式選單歸成0
    all_ddls.each(function () {
        $(this).find("option").each(function (Idx) {
        if (Idx==0) {
            $(this).attr("selected", true);
            }
        });

清空Textbox


var all_Inputs = $("input[type=textbox]");
all_Inputs.val("");

清空checkbox(視jQuery的版本):


//這個版本比較舊的,多少版我忘了
$('input:checkbox').attr("checked", "");
//這個版本比較新,多少版我忘了
$("input[type='checkbox']").attr("checked", false);

清空radioButton


 //不知道為啥下面這個first-child老是會選到每組radio button的最後一個元素,
//而且我反過來用last-child的話,就jQuery完全沒反應,
//只好改用.each的方式,根據不同的radio button的name去抓不同組的radio button
//$("input[type=radio]:first-child").attr("checked",true);
var all_rbl = $("input[type=radio]");
all_rbl.each(function () {
    var rbl_name = $(this).attr("name");
    $("input[type=radio][name*='" + rbl_name + "']:first").attr("checked", true)
});

清除textarea值 


 $('textarea').attr("value", "");