简体   繁体   中英

how do i clear a textfield in backbone.js after i click the button?

my input is in HTML file my button is here in js file. Im a beginner here please teach me. Im still confuse of backbone.js

 var ListView = Backbone.View.extend({
    el: $('body'),
    events: {
        'click button#add': 'addItem'

    },
    initialize: function () {
        _.bindAll(this, 'render', 'addItem', 'appendItem');

        this.collection = new List();
        this.collection.bind('add', this.appendItem);

        this.counter = 0;
        this.render();
    },

    render: function () {
        $(this.el).append("<button id='add' class='btn btn-primary clear'>Add Items</button>");
        $(this.el).append("<ul class='ul_adjust'></ul>");
        _(this.collection.model).each(function (item) {
            appendItem(item);
        }, this);
    },

    addItem: function () {

        var item = new Item();
        item.set({
            part1: $("#name").val()
        });

        this.collection.add(item);
    },
    appendItem: function (item) {
        var itemView = new ItemView({
            model: item
        });

        $('ul', this.el).append(itemView.render().el);
    },


});

did you get it? i remove some of not necessary but i think this will be inserted somewhere here.

Your events is setup so that when you click the #add button, it calls the addItem function. So you just need to clear the textbox #name in this same function.

I would simply do:

$("#name").val("");

at the end of your addItem function, after this.collection.add(item); so the value is only cleared when you no longer need it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM