Javascript 函数没有被触发?

发布于 2025-01-04 16:09:13 字数 2163 浏览 1 评论 0原文

在我的 MVC 3 应用程序中,我使用 CKeditor 来编辑富文本。现在我需要能够使用 HTML 标签将文本保存到数据库。

问题是,当我单击“保存”按钮时,没有任何反应。

警报返回未定义。

查看:

@using (Html.BeginForm()) {
    @Html.ValidationSummary(true)
    <fieldset>
        @Html.DropDownListFor(model=>model.Id, Model.Texts)  
        <div class="editor-field">
            @Html.TextAreaFor((model => model.Text), new { @Id = "editor1" })
            @Html.ValidationMessageFor(model => model.Text)
        </div> 
        <script type="text/javascript">
            CKEDITOR.replace('editor1');
            </script>

        <script type="text/javascript">
            var editor_data1 = $('editor1').val(); 
            var editor_data = editor_data1.getHtml();
            var url = '@Url.Action("EditText", "Admin")';
            var data = { commentText: editor_data };

            function Save() {
                alert(editor_data);
                $.post(url, data, function(result) {  

});

            };
            $('#Id').change(function () {
                var selectedText = $(this).val();
                if (selectedText != null && selectedText != '') {
                    $.getJSON('@Url.Action("Text","Admin")', { Id: selectedText }, function (text) {

//                        var textSelect = $('#Text');
//                        textSelect.empty();
//                        $("#Text")[0].value = text;
                        CKEDITOR.instances['editor1'].setData(text);
                        //                $.each(text, function (index, employee) {
                        //                    textSelect.append($('<option/>', {
                        //                        value: employee.Value,
                        //                        text: employee.Text
                        ////                    }));
                        //                });
                    });

                }
            });
</script>
    </fieldset>
     <p>
            <input type="button" value="Save"  onclick="Save()"/>
        </p>
} 

In my MVC 3 app I am using CKeditor for editing rich text. Right now I need to be able to save text to DB with HTML tags.

Problem is that on my click of button 'Save' nothing happens.

Alert returns Undefined.

View:

@using (Html.BeginForm()) {
    @Html.ValidationSummary(true)
    <fieldset>
        @Html.DropDownListFor(model=>model.Id, Model.Texts)  
        <div class="editor-field">
            @Html.TextAreaFor((model => model.Text), new { @Id = "editor1" })
            @Html.ValidationMessageFor(model => model.Text)
        </div> 
        <script type="text/javascript">
            CKEDITOR.replace('editor1');
            </script>

        <script type="text/javascript">
            var editor_data1 = $('editor1').val(); 
            var editor_data = editor_data1.getHtml();
            var url = '@Url.Action("EditText", "Admin")';
            var data = { commentText: editor_data };

            function Save() {
                alert(editor_data);
                $.post(url, data, function(result) {  

});

            };
            $('#Id').change(function () {
                var selectedText = $(this).val();
                if (selectedText != null && selectedText != '') {
                    $.getJSON('@Url.Action("Text","Admin")', { Id: selectedText }, function (text) {

//                        var textSelect = $('#Text');
//                        textSelect.empty();
//                        $("#Text")[0].value = text;
                        CKEDITOR.instances['editor1'].setData(text);
                        //                $.each(text, function (index, employee) {
                        //                    textSelect.append($('<option/>', {
                        //                        value: employee.Value,
                        //                        text: employee.Text
                        ////                    }));
                        //                });
                    });

                }
            });
</script>
    </fieldset>
     <p>
            <input type="button" value="Save"  onclick="Save()"/>
        </p>
} 

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

空‖城人不在 2025-01-11 16:09:13

请尝试在 jQuery 中将:替换

var editor_data1 = $('editor1').val();

var editor_data1 = $('#editor1').val();

如果您想执行 id 选择器, :,您应该在其前面添加前缀<代码>#。

此外,您使用的 .val() 方法也会返回一个字符串。我不知道 CKEditor 但我非常怀疑字符串类型上有 getHtml

您可能的意思是:

var editor_data = CKEDITOR.instances['editor1'].getData();

此外,您似乎正在使用 id="Id" 订阅某些 DOM 元素的更改事件:

$('#Id').change(...);

我在您的 DOM 中看不到任何此类元素。您确定您的意思不是:

$('#editor1').change(...);

或其他一些元素吗?也许是下拉菜单?

您似乎对 jQuery 语法有一些问题。我建议您阅读一些教程。

Try replacing:

var editor_data1 = $('editor1').val();

with:

var editor_data1 = $('#editor1').val();

in jQuery if you want to perform an id selector you should prefix it with #.

Also the .val() method that you are using returns a string. I am not aware with CKEditor but I very highly doubt that there is a getHtml on the string type.

You probably meant:

var editor_data = CKEDITOR.instances['editor1'].getData();

Also you seem to be subscribing to the change event of some DOM element with id="Id":

$('#Id').change(...);

I can't see any such element in your DOM. Are you sure that you didn't mean:

$('#editor1').change(...);

or some other element? Maybe the dropdown?

You seem to have some issues with the jQuery syntax. I would recommend you going through some tutorials.

半岛未凉 2025-01-11 16:09:13

Save() 函数上面的行将在解析文档时执行。那时,您的 CKEditor 可能还不存在。要解决此问题,请移动到 Save() 内部,如下所示:

        function Save() {
        var editor_data1 = $('editor1').val(); 
        var editor_data = editor_data1.getHtml();
        var url = '@Url.Action("EditText", "Admin")';
        var data = { commentText: editor_data };
        alert(editor_data);
        $.post(url, data, function(result) {  });

The lines above the Save() function will execute when the document is being parsed. At that point, your CKEditor may not even exist yet. To fix this, move then inside Save(), like this:

        function Save() {
        var editor_data1 = $('editor1').val(); 
        var editor_data = editor_data1.getHtml();
        var url = '@Url.Action("EditText", "Admin")';
        var data = { commentText: editor_data };
        alert(editor_data);
        $.post(url, data, function(result) {  });
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文