点击图标打开jQuery UI Datepicker

On click of icon open jQuery UI Datepicker

本文关键字:jQuery UI Datepicker 图标      更新时间:2023-09-26

在主干视图中,我在html

中有以下代码
<input type="text" id="fromDate" name="fromDate"/><a id="cal">
<img src="img/calendar.gif"></a>
在view的js文件中,我有以下代码:
define(['jquery', 'underscore', 'backbone', 'text!views/page1/page1.tpl'], function($, _, Backbone, tmpl_page1View) {
var page1View = Backbone.View.extend({
    // Setting the view's template property using the Underscore template method        
    template: _.template(tmpl_page1View),
    // View constructor
    initialize: function() {
        self = this;
    },
    // View Event Handlers
    events: {
        "click #page2": "clickedPage2",
        "click #cal":"calClicked"
    },
    // Renders the view's template to the UI
    render: function() {
        this.$el.html(this.template({data: this.templateData}));           
        // Maintains chainability
        return this;
    },
    clickedPage2:function(){
        window.location.href = "#page2"
    },
    calClicked:function(){
       $("#fromDate").datepicker({
          showOn: "button",
          buttonImage: "img/calendar.gif",
          buttonImageOnly: true
        });
    }
});
return page1View;
});

单击日历图标的事件,我想打开日期选择器,但它不工作。你能帮我解决这个问题吗?谢谢你。

您应该初始化日期选择器,例如在render方法中,日期选择器将在单击按钮时自动打开,因此您根本不需要calClicked

var page1View = Backbone.View.extend({
// Setting the view's template property using the Underscore template method        
template: _.template(tmpl_page1View),
// View constructor
initialize: function() {
    self = this;
},
// View Event Handlers
events: {
    "click #page2": "clickedPage2",
    "click #cal":"calClicked"
},
// Renders the view's template to the UI
render: function() {
    this.$el.html(this.template({data: this.templateData}));  
    // init datepicker
    this.$("#fromDate").datepicker({
      showOn: "button",
      buttonImage: "img/calendar.gif",
      buttonImageOnly: true
    });

    // Maintains chainability
    return this;
},
clickedPage2:function(){
    window.location.href = "#page2"
}    
});
return page1View;
});