修改此代码以在窗口上运行.加载而不是表单提交

modifying this code to run on window.load instead of on form submit

本文关键字:加载 表单提交 运行 代码 窗口 修改      更新时间:2023-09-26

我对web开发很陌生,虽然我已经把我的头包裹在javascript周围,但我试图修改一些代码,我抓住了github上用jquery编写的我自己的个人使用。基本上现在代码是从输入表单onsubmit执行的,但是我计划在代码发送到客户端执行之前在服务器上用php填写表单数据,所以我想修改下面的代码以在window.load上运行。我已经缩小了需要修改的东西,从几百行代码到下面代码块中的render: function(){中的某个地方,但我不确定如何准确地做到这一点,并将输入正确地传递给应用程序

中的其他函数
var InputView = Backbone.View.extend({
    initialize: function() {
        _.bindAll(this, 'create');
        this.template = _.template($('#input_template').html());
    },
    render: function() {
        this.$el.html(this.template({}));
        this.$el.find('form').submit(_.bind(function(event) {
             window.location = '#' + this.$el.find('input').val();
        }, this));
        this.$el.find('#create').click(this.create);
        this.$el.find('#create').on('click', this.create, this);
        return this;
    },

基本上我只需要知道如何正确地将this.$el.find('input').val();绑定到php echo…很抱歉外包,但我真的不明白到底是怎么回事在所有这些代码,所以任何帮助,这将是非常感谢

这里是InputView函数的其余部分,以防它是相关的,但我认为它不是

    create: function(event) {
        if(this.$el.find('#create').hasClass('disabled')) return;
        var button = this.$el.find('#create');
        button.addClass('disabled');
        event.preventDefault();
        var product = 'Torque';
        var btapp = new Btapp;
        btapp.connect({
            queries: [['btapp', 'create'], ['btapp', 'browseforfiles']],
            product: product,
            poll_frequency: 500
        });

        var status = new Backbone.Model({
            btapp: btapp,
            product: btapp.get('product'),
            status: 'uninitialized'
        });
        var status = new StatusView({model: status});
        $('.toolbox').append(status.render().el);
        var browse_ready = function() {
            btapp.off('add:bt:browseforfiles', browse_ready);
            btapp.trigger('input:waiting_for_file_selection');
            btapp.browseforfiles(function(files) {
                var files = _.values(files);
                if(files.length === 0) {
                    btapp.trigger('input:no_files_selected');
                    setTimeout(function() {
                        btapp.disconnect();
                        status.destroy();
                        button.removeClass('disabled');
                    }, 3000);
                    return;
                }
                var create_callback = function(data) {
                    btapp.disconnect();
                    btapp.trigger('input:torrent_created');
                    setTimeout(_.bind(btapp.trigger, btapp, 'input:redirecting'), 1000);
                    setTimeout(function() {
                        status.destroy();
                        window.location = '#' + data;
                    }, 3000);
                }
                var torrent_name = create_torrent_filename(files);
                console.log('btapp.create(' + torrent_name + ', ' + JSON.stringify(files) + ')');
                btapp.create(torrent_name, files, create_callback);
                btapp.trigger('input:creating_torrent');
            });
        };
        btapp.on('add:bt:browseforfiles', browse_ready);
        btapp.on('all', _.bind(console.log, console));
    }
});

所以我基本上解决了我自己的冗长的问题所有我正在寻找的是改变这一行

this.$el.find('form').submit(_.bind(function(event) {

this.$el.ready(_.bind(function(event) {

无论如何可能没有人回答,因为它开始我实际上学习一些jquery语法我已经放了很长时间了:)