我有一个Jquery模式表单通过ajax创建一个表单,在这个表单中我插入一个按钮,用于克隆选择并检查副本。 当我第一次使用它时它完美地工作但如果我第二次调用模态,则单击事件会触发两次。 模态和单击声明位于名为
的ajax文件中这是带控制功能的点击声明
$("#addfonte").unbind("click");
$("#addfonte").off("click");
$(document).on("click","#addfonte",function(e){
e.preventDefault();
var current = $("#IdAddFonte").val();
var index = $(".newfonte").length + 1;
console.log("indice"+index);
var id = $(this).attr('id');
var select = $('.newfonte option:selected');
var presente=false;
console.log("quante ne ho "+select.length);
//Clone the DropDownList
var ddl = $("#IdAddFonte").clone();
//Set the ID and Name
var newname="AddFonte_"+index;
var newbutton="DelButton_"+index;
ddl.attr("id", newname);
ddl.attr("name", newname);
ddl.attr("class","newfonte");
//[OPTIONAL] Copy the selected value
var selectedValue = $("#IdAddFonte option:selected").val();
ddl.find("option[value = '" + selectedValue + "']").attr("selected", "selected");
select.each(function(){
var sameid="#"+$(this).parent().attr('id');
console.log("stesso id "+sameid+" valore "+$(this).val()+" riferimento "+ddl.attr("id"))
$(sameid).removeClass("evidenziato");
$('#error').html("").removeClass("error");
if ($(this).val()==current){
presente = true;
$('#error').html("Fonte già presente").addClass("error");
$(sameid).addClass("evidenziato");
}
});
if (!presente){
//Append to the DIV.
$("#fonti").prepend(ddl);
//$("#fonti").prepend("<br /><br />");
var select = $('.newfonte option:selected');
var valori="";
$("#fonti .btdel").remove();
select.each(function(index){
selected=$(this).parent();
//Set the ID and Name
var newname="AddFonte_"+index;
selected.attr("id", newname);
selected.attr("name", newname);
selected.attr("class","newfonte");
var newbutton="DelButton_"+index;
var bottone='<button id="';
bottone+=newbutton+'" class="btdel">';
bottone+='<img height="20px" src="images/Delete_Icon.png" alt="cancella fonte"></button>';
$("#"+newname).after(bottone);
});
elencofonti(select);
$(document).unbind("click","#fonti .btdel");
$(document).on("click","#fonti .btdel",function(e){
var select = $('.newfonte option:selected');
e.preventDefault();
$(this).prev().remove();
$(this).remove();
elencofonti(select);
});
}
这是模态声明
var dialog,form;
dialog =$('#ModalB').dialog({
width: "auto",
modal:true,
title:"Inserisci anno di riferimento",
open:function(){
$('input:submit').hide();
setTimeout(function(){$('#IdFunzione').focus();},50);
originalContent = $("#ModalFormB").html();
},
close:function(dialog){
console.log("chiusa la form");
$("#ModalFormB").html(originalContent);
},
buttons:{
<?php if ($dati){?>
"Modifica":function(){
$(this).dialog("close");
ModificaImpianto();
$("#ModalFormB").remove();
$(this).dialog("destroy");
//creaquestionario(idimpianto,idanagrafica,idutente,$('#ModalAnno').val());
//dialog.find( "form" ).submit();
},
<?php }else{?>
"Aggiungi":function(){
$(this).dialog("close");
AggiungiImpianto();
$("#ModalFormB").remove();
$(this).dialog("destroy");
//creaquestionario(idimpianto,idanagrafica,idutente,$('#ModalAnno').val());
//dialog.find( "form" ).submit();
},<?php } ?>
"Annulla": function() {
$("#ModalFormB").remove();
dialog.dialog( "close" );
$(this).dialog("destroy");
}
}
}).css("font-size", "0.7em");
form = dialog.find( "form" ).on( "submit", function( event ) {
event.preventDefault();
dialog.dialog( "close" );
alert($("#ModalFormB").serialize());
});
});