简体   繁体   English

自动调用点击处理程序以在页面加载时加载jQuery对话框

[英]Automatically call a click handler to load a jQuery dialog box on page load

I've got a page, on the page there is a button wrapped in an anchor tag: 我有一个页面,页面上的锚标记中包裹着一个按钮:

<a href="javascript:void(0)" data-bind="click: AddNewThread, text: 'Add New Discussion'" class="btn new"></a>

This calls a function labeled as AddNewThread , now this function opens up a dialog Box using jQuery, and in the dialog box there is a text field that you enter text to, and this text is then pasted on the page using ajax. 这将调用标记为AddNewThread的函数,现在该函数使用jQuery打开一个对话框,并且在对话框中有一个文本字段,您可以在其中输入文本,然后使用ajax将这些文本粘贴到页面上。

The problem: I want to remove the click event entirely; 问题:我想完全删除click事件; I want to have it on page load, it will load the text box, and the functionality is remaining the same, But I just don't know how to go about this. 我想在页面加载时加载它,它将加载文本框,并且功能保持不变,但是我只是不知道该怎么做。

Here's the code for the AddNewThread function: 这是AddNewThread函数的代码:

function DiscussionViewModel() {
    var self = this;
    self.New = ko.mapping.fromJS(newDiscussion);
    self.Threads = ko.mapping.fromJS(threads, mapping);
    self.AddNewThread = function () {
        $("#dvAddDiscussion").dialog('open');
        ko.mapping.fromJS(newDiscussion, self.New);

        if (CKEDITOR.instances["txtDiscussioinDescription"] != undefined) {
            CKEDITOR.instances["txtDiscussioinDescription"].setData('');
        }
    };
    self.AddReply = function (thread) {
        $("#txtDiscussioinDescription").val('');
        self.LastDiscussionId(thread.New.ParentId());
        self.New.Title('');
        self.New.DiscussionId(thread.New.DiscussionId());
        self.New.Type(thread.New.Type());
        self.New.TypeId(thread.New.TypeId());
        self.New.Description('');
        self.New.ParentId(thread.New.ParentId());
        self.New.UserId(thread.New.UserId());
        self.New.User(thread.New.User());
        $("#dvAddDiscussion").dialog('open');

    };
    self.LastDiscussionId = ko.observable();
    self.SubmitReply = function (form) {
        var jForm = $(form);
        $("#dvAddDiscussion").dialog('close');
        var request = jForm.serialize();
        var discussionId = $("#DiscussionID").val();
        var parentId = $("#ParentId").val();
$.post(addDiscussionUrl, request, function (response) {
            if (response) {
                if ((discussionId == '' || discussionId == undefined) && parentId == '') {
                    $.post(summariesUrl, { type: type, objectId: objectId }, function (response) {
                        ko.mapping.fromJS(response, mapping, self.Threads);
                        $("abbr.timeago").timeago();
                    });
                } else {
                    self.LoadReplies(self.LastDiscussionId());
                }

            } else {
                alert("Error occured while adding discussion");
            }
        });


    };
    self.GetReplies = function (data) {
        var thread;

        if (data.IsMaximized() == true) {
            for (var i = 0; i < self.Threads().length; i++) {
                if (self.Threads()[i].DiscussionId() == data.DiscussionId()) {
                    self.Threads()[i].IsMaximized(false);
                    break;
                }
            }
        } else {
            for (var i = 0; i < self.Threads().length; i++) {
                if (self.Threads()[i].DiscussionId() == data.DiscussionId()) {
                    self.Threads()[i].IsMaximized(true);
                    self.LoadReplies(data.DiscussionId());
                    break;
                }
            }
        }

    };
    self.LoadReplies = function (discussionId) {
        var request = {
            discussionId: discussionId,
            type: type,
            objectId: objectId
        };
        $.post(threadsUrl, request, function (response) {
            for (var i = 0; i < self.Threads().length; i++) {
                if (self.Threads()[i].DiscussionId() == discussionId) {
                    self.Threads()[i].Replies.splice(0, self.Threads()[i].Replies().length);
                    for (var j = 0; j < response.length; j++) {
                        self.Threads()[i].Replies.push(new ReplyModel(response[j]));
                    }
                    break;
                }
            }
            $("abbr.timeago").timeago();
        });
    };
    self.EditReply = function (dicussion) {
        self.New.Title(dicussion.Title());
        self.New.DiscussionId(dicussion.DiscussionId());
        self.New.Type(dicussion.Type());
        self.New.TypeId(dicussion.TypeId());
        self.New.Description(dicussion.Description());
        self.New.ParentId(dicussion.ParentId());
        self.New.UserId(dicussion.UserId());
        self.New.User(dicussion.User());
        self.LastDiscussionId(dicussion.DiscussionId());
        $("#dvAddDiscussion").dialog('open');
    };
    self.ToggleView = function (data) {
        for (var i = 0; i < self.Threads().length; i++) {

            for (var j = 0; j < self.Threads()[i].Replies().length; j++) {
                if (self.Threads()[i].Replies()[j].DiscussionId == data.DiscussionId) {
                    self.Threads()[i].Replies()[j].HasMore(!self.Threads()[i].Replies()[j].HasMore());
                    break;
                }

            }
        }
    };
}
var discussionViewmodel = new DiscussionViewModel();
ko.applyBindings(discussionViewmodel, document.getElementById("dvThreads"));

$(function () {
    $("#accordion").accordion();
    var dialog = $("#dvAddDiscussion").dialog({
        autoOpen: false,
        width: 720,
        height: 500,
        title: 'Discussion',
        modal: true,
        open: function () {
            if (typeof DISABLE_FOR_DIALOG != 'undefined')
                DISABLE_FOR_DIALOG = true;
            CKEDITOR.replace('txtDiscussioinDescription');

        },
        close: function (parameters) {
            if (typeof DISABLE_FOR_DIALOG != 'undefined')
                DISABLE_FOR_DIALOG = false;
            if (CKEDITOR.instances["txtDiscussioinDescription"] != undefined) {
                CKEDITOR.instances.txtDiscussioinDescription.updateElement();
                CKEDITOR.instances.txtDiscussioinDescription.destroy();
            }

        }
    });
    dialog.parent().appendTo($("#dvDialogs"));
    $("#frmAddDiscussion").data("validator").settings.submitHandler = discussionViewmodel.SubmitReply;
    $("#frmAddDiscussion").bind('click', function () {
        CKEDITOR.instances.txtDiscussioinDescription.updateElement();
    });
});


</script>

You just need to call the function at the appropriate time, which in this case is within your jQuery ready handler, right after this block: 您只需要在适当的时间调用该函数,在这种情况下,该时间在您的jQuery ready处理程序内,紧接在此块之后:

$("#frmAddDiscussion").bind('click', function () {
    CKEDITOR.instances.txtDiscussioinDescription.updateElement();
});

You should be able to just add: 您应该能够添加:

discussionViewmodel.AddNewThread();

This will invoke the function the same way that knockout would have done through the click data binding. 这将与click数据绑定中的敲除操作相同的方式调用该函数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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