我尝试通过按钮在tinyMCE编辑器中插入Wordpress shortocde,我有一点问题。脚本多次插入短代码。我很难解释它,所以我录制了一段短片:
http://www.youtube.com/watch?v=bJJMkAXKNVM
这是一个完整的JavaScript代码:
jQuery(document).ready(function($){
$("#wpwrap").append("<div class=\"avgrund-cover\"></div>");
$("#close-modal").click(function(){
Avgrund.hide("#acf-popup");
});
(function() {
tinymce.create('tinymce.plugins.acfshortcode', {
init : function(ed, url) {
ed.addButton('acfshortcode', {
title : 'Ajax Contact Form',
image : url+'/../acf-button.png',
onclick : function() {
var showname = true;
var showsubject = true;
var showbox = true;
var formwidth = $("#form-width").val();
Avgrund.show("#acf-popup");
$("#disable_name").click(function(e){
if (showname) {
$("#name").parent().slideUp('200');
showname = false;
}else{
$("#name").parent().slideDown('200');
showname = true;
}
});
$("#close-modal").click(function(e){
Avgrund.hide("#default-popup");
});
$("#disable_subject").click(function(e){
if (showsubject) {
$("#subject").parent().slideUp('200');
showsubject = false;
}else{
$("#subject").parent().slideDown('200');
showsubject = true;
}
});
$("#disable_box").click(function(e){
if (showbox) {
$("#acf-contact-form").removeClass('acf-box');
showbox = false;
}else{
$("#acf-contact-form").addClass('acf-box');
showbox = true;
}
});
$("#form-width").keyup(function() {
formwidth = $("#form-width").val();
$("#acf-contact-form").css('width', formwidth+'px');
});
$("#insert-acf-form").click(function(e){
var showname2, showsubject2;
if (showname == true) showname2 = 'yes'; else showname2 = 'no';
if (showsubject == true) showsubject2 = 'yes'; else showsubject2 = 'no';
if (showbox == true) box2 = 'yes'; else box2 = 'no';
var name_label = $("#name").val();
var email_label = $("#email").val();
var subject_label = $("#subject").val();
var message_label = $("#message-form").val();
var button_label = $("#acf-send-button").text();
if (formwidth < 140 ) formwidth = 140;
if (formwidth > 1000 ) formwidth = 1000;
ed.execCommand('mceInsertContent', false, '<p>[acf_contact_form width="'+formwidth+'px" box="'+box2+'" name="'+showname2+'" subject="'+showsubject2+'" name_label="'+name_label+'" email_label="'+email_label+'" subject_label="'+subject_label+'" message_label="'+message_label+'" button_text="'+button_label+'"]</p>');
Avgrund.hide("#default-popup");
e.preventDefault();
});
}
});
},
createControl : function(n, cm) {
return null;
},
getInfo : function() {
return {
longname : "Ajax Contact Form Shortcode",
author : 'Jacek Jagiello',
version : "1.0"
};
}
});
tinymce.PluginManager.add('acfshortcode', tinymce.plugins.acfshortcode);
})();
});
以上是在编辑器中插入短代码的函数表单代码:
(...) onclick : function() {
var showname = true;
var showsubject = true;
var showbox = true;
var formwidth = $("#form-width").val();
Avgrund.show("#acf-popup");
$("#disable_name").click(function(e){
if (showname) {
$("#name").parent().slideUp('200');
showname = false;
}else{
$("#name").parent().slideDown('200');
showname = true;
}
});
$("#close-modal").click(function(e){
Avgrund.hide("#default-popup");
});
$("#disable_subject").click(function(e){
if (showsubject) {
$("#subject").parent().slideUp('200');
showsubject = false;
}else{
$("#subject").parent().slideDown('200');
showsubject = true;
}
});
$("#disable_box").click(function(e){
if (showbox) {
$("#acf-contact-form").removeClass('acf-box');
showbox = false;
}else{
$("#acf-contact-form").addClass('acf-box');
showbox = true;
}
});
$("#form-width").keyup(function() {
formwidth = $("#form-width").val();
$("#acf-contact-form").css('width', formwidth+'px');
});
$("#insert-acf-form").click(function(e){
var showname2, showsubject2;
if (showname == true) showname2 = 'yes'; else showname2 = 'no';
if (showsubject == true) showsubject2 = 'yes'; else showsubject2 = 'no';
if (showbox == true) box2 = 'yes'; else box2 = 'no';
var name_label = $("#name").val();
var email_label = $("#email").val();
var subject_label = $("#subject").val();
var message_label = $("#message-form").val();
var button_label = $("#acf-send-button").text();
if (formwidth < 140 ) formwidth = 140;
if (formwidth > 1000 ) formwidth = 1000;
ed.execCommand('mceInsertContent', false, '<p>[acf_contact_form width="'+formwidth+'px" box="'+box2+'" name="'+showname2+'" subject="'+showsubject2+'" name_label="'+name_label+'" email_label="'+email_label+'" subject_label="'+subject_label+'" message_label="'+message_label+'" button_text="'+button_label+'"]</p>');
Avgrund.hide("#default-popup");
e.preventDefault();
});
}
答案 0 :(得分:1)
您正在按钮的onclick
处理程序中为弹出窗口添加事件处理程序。这意味着每次单击按钮时都会添加它们。
换句话说,第二次通过单击按钮打开弹出窗口时,#insert-acf-form
上有两个点击处理程序。第三次你有三个,等等。你分配的所有其他事件处理程序也是如此 - 但只有点击处理程序会立即显现,因为mceInsertContent
将被调用两次。
一个解决方案:由于在第一次单击按钮之前似乎已经创建了弹出窗口,因此可以在onclick事件处理程序之外添加所有事件处理程序 - 即,在初始化代码中(注意,代码是只是为了证明这个想法,它是不测试,可能包括语法错误和诸如此类的东西):
jQuery(document).ready(function($) {
$("#wpwrap").append("<div class=\"avgrund-cover\"></div>");
$("#close-modal").click(function() {
Avgrund.hide("#acf-popup");
});
(function() {
var showname = true;
var showsubject = true;
var showbox = true;
var formwidth = $("#form-width").val();
// ********** MOVE REGISTRATION OF POPUP EVENT HANDLERS HERE **********
tinymce.create('tinymce.plugins.acfshortcode', {
init : function(ed, url) {
ed.addButton('acfshortcode', {
title : 'Ajax Contact Form',
image : url+'/../acf-button.png',
onclick : function() {
Avgrund.show("#acf-popup");
// ************* NOTE, NO EVENT HANDLERS HERE ***********
}
});
},
createControl : function(n, cm) {
return null;
},
// etc.
});
})();
}
或者,您可以在onclick
中添加一个检查,以查看之前是否已调用onclick
,并且在这种情况下不再添加事件处理程序。