Javascript 函数没有被触发?
在我的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
请尝试在 jQuery 中将:替换
为
如果您想执行 id 选择器, :,您应该在其前面添加前缀<代码>#。
此外,您使用的
.val()
方法也会返回一个字符串。我不知道 CKEditor 但我非常怀疑字符串类型上有getHtml
。您可能的意思是:
此外,您似乎正在使用
id="Id"
订阅某些 DOM 元素的更改事件:我在您的 DOM 中看不到任何此类元素。您确定您的意思不是:
或其他一些元素吗?也许是下拉菜单?
您似乎对 jQuery 语法有一些问题。我建议您阅读一些教程。
Try replacing:
with:
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 agetHtml
on the string type.You probably meant:
Also you seem to be subscribing to the change event of some DOM element with
id="Id"
:I can't see any such element in your DOM. Are you sure that you didn't mean:
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.
Save()
函数上面的行将在解析文档时执行。那时,您的 CKEditor 可能还不存在。要解决此问题,请移动到Save()
内部,如下所示: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 insideSave()
, like this: