这似乎是直截了当的,但可能我错过了一些东西。我尝试在列表中绑定每个项目的ondblclick事件,并将其值传递给编辑器,以便在单击该项目时将其文本连接到编辑器中。出于某种原因,只有第一项保留追加到编辑文本,甚至当我点击第二或第三项..将ondblclick事件绑定到列表中的每个项目并在事件中传递该项目的值
$('#listComments').each(function (index, item) {
$('#listComments')[0][i].ondblclick = function() {
alert(i);
var editor = $("#editor").data("kendoEditor");
editor.exec("inserthtml", { value: commentSet[i] });
};
commentsSet是一个充满原始值如下
var commentsSet = [];
$.ajax({
url: urlComments,
data: { headerId: $('#ddlCommentHeaders').val().toString() },
dataType: "json",
success: function (result) {
$('#listComments').empty();
commentSet = [];
$.each(result, function (index, value) {
commentSet[index] = value.comment;
$('#listComments').append($('<option>').text($(value.comment).text()).val(value.CommentID));
});
for (var i = 0; i < $('#listComments')[0].length; i++) {
$('#listComments')[0][i].ondblclick = function() {
alert(i);
var editor = $("#editor").data("kendoEditor");
editor.exec("inserthtml", { value: commentSet[i] });
};
}
}
});
另一个临时数组
基本上value.comment有一个带有html标签的文本,我需要在编辑器中,但要显示在列表项中,我只需要纯文本没有html标签,所以我已经用html文本填充了一个临时数组,并将列表与纯文本值。 ondblclick事件仅出现在列表的第一项中。 我希望我能够解释我的问题。 :)
UPDATE
在浏览器当i型$( '#listComments')
[选择#listComments.classComments,上下文:文档,选择器: “#listComments”] 0:选择#listComments.classComments上下文: 文件长度:1个选择器: “#listComments” 原:对象(0)
$( '#listComments')。长度为1
$( '#listComments')[0]
<select class="classComments" id="listComments" multiple="multiple" name="cM.CommentsList" style="height:105px; margin-bottom:5px; max-width:100%;"><option value="1382" style="white-space: pre-wrap;">This is a Math Comment</option><option value="1383">This is a second Comment</option></select>
$( '#listComments')[0]。长度为2
HTML
<div style="margin-left:5px; width:80%" class=" col-xs-9">
@Html.ListBoxFor(m => m.cM.CommentsList, new SelectList(Model.cM.CommentsList, "CommentID", "Comment"), new { @id = "listComments", @style = "height:105px; margin-bottom:5px; max-width:100%;", @class="classComments" })
</div>
Jquery的每个功能
$('#listComments').each(function (index, item) {
item.ondblclick = function() {
alert(index);
editor.exec("inserthtml", { value: commentSet[index] });
};
});
回答
虽然两者穆罕默德·优素福也给予了正确的解决办法,我只能标出正确的答案之一。我也提到this的帖子。是这样做的......感谢卢克的不断支持。
$('#listComments').dblclick(function(){
$("#listComments option:selected").each(function() {
var index = $(this).index();
var editor = $("#editor").data("kendoEditor");
editor.exec("inserthtml", { value: commentSet[index] });
});
});
先看看.. ID必须是独特的使用类,而不要使用相同的身份证多个元素* –
用.classComments替换所有#listComments ...相同的行为 – Samra
编辑你的HTML与呈现的HTML ..我宁愿不混合jquery与纯js –