1. 程式人生 > >jQuery EasyUI 1.3.2 日期控制元件 加入清空按鈕

jQuery EasyUI 1.3.2 日期控制元件 加入清空按鈕

在使用easyui日期外掛時,為了規範輸入一般需要對輸入框新增只讀屬性禁止輸入。但如果日期值是可選時就會出現問題:無法清空值。旁邊再製作專門清空按鈕未免太費事,所以可以對原始碼改造一下。以下以jQuery EasyUI 1.3.2為例。

開啟jquery.easyui.min.js 約10907行:

var _806=$("<div class=\"datebox-button\"></div>").appendTo(_804);
$("<a href=\"javascript:void(0)\" class=\"datebox-current\"></a>").html(opts.currentText).appendTo(_806);
$("<a href=\"javascript:void(0)\" class=\"datebox-close\"></a>").html(opts.closeText).appendTo(_806);
var cl=$("<a href=\"javascript:void(0)\" class=\"datebox-clean\"></a>").html(opts.cleanText).appendTo(_806);
_806.find(".datebox-current,.datebox-close").hover(function(){
$(this).addClass("datebox-button-hover");
},function(){
$(this).removeClass("datebox-button-hover");
});
_806.find(".datebox-current").click(function(){
_802.calendar.calendar({year:new Date().getFullYear(),month:new Date().getMonth()+1,current:new Date()});
});
_806.find(".datebox-close").click(function(){
$(_801).combo("hidePanel");
});
cl.click(function(){
    _809(_801,"");//或$(_801).combo("setValue","").combo("setText","");
    $(_801).combo("hidePanel");
});

然後在10990行左右:
}},currentText:"Today",closeText:"Close",okText:"Ok",cleanText:"Clean",formatter:function(date){

說明:紅色的為新增的指令碼,其中_809(_801,"");是呼叫easyUI內建日期賦值函式。$(_801).combo("setValue","")是對真實input賦值,$(_801).combo("setText","")是對外掛賦值。

然後開啟:themes/default/easyui.css 約1440行:

.datebox-current,
.datebox-close,
.datebox-ok,.datebox-clean {
  text-decoration: none;
  font-weight: bold;
  opacity: 0.6;
  filter: alpha(opacity=60);
}
.datebox-clean{float:center}
/*再下幾行.........*/
.datebox-current,
.datebox-close,
.datebox-ok,.datebox-clean {
  color: #444;
}

說明:如果你使用的是其他風格,同理修改其他easyui.css檔案。最後開啟locale/easyui-lang-zh_CN.js約40行:
$.fn.datebox.defaults.cleanText = '清空';