点击图标打开jQuery UI Datepicker

时间:2014-05-05 06:49:37

标签: javascript jquery backbone.js jquery-ui-datepicker

在骨干视图中,我在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;
});

在日历图标的点击事件中,我想打开datepicker但它不起作用。你能帮我解决这个问题。谢谢。

1 个答案:

答案 0 :(得分:1)

你应该初始化datepicker,例如在render方法中,datepicker会在按钮点击时自动打开,因此您根本不需要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;
});