我有一个javascript文件,我使用html标签创建表单。在此表单上有2个按钮:确认和取消。当用户点击取消时,表格将被丢弃。我编写了在同一个javascript文件中关闭表单的方法,该文件包含动态创建表单元素的代码。但是,当我尝试关闭表单时,我在控制台上收到以下错误。
调用方法的Javascript按钮
...
$("#lot").append(
"<div class='form-group'></br>"+
"<label class='col-md-12 control-label' for='tableFormConfirmButton'></label></br>"+
"<label class='col-md-12 control-label' for='tableFormConfirmButton'></label></br>"+
"<label class='col-md-12 control-label' for='tableFormConfirmButton'></label></br>"+
"<label class='col-md-2 control-label' for='tableFormConfirmButton'></label>"+
" <button id='tableFormConfirmButton' name='tableFormConfirmButton' class='btn btn-primary' onclick='storeTableFormInfo("+newAgent+","+i+","+e+","+mouseTop+","+mouseLeft+","+elemType+")'>Confirm</button>"+
"  <button id='tableFormCancelButton' name='tableFormCancelButton' class='btn btn-danger' onclick='closeForm()'>Cancel</button>"+
"</div></br>"
);
...
同一文件中的closeForm()方法
function closeForm()
{
var myNode = document.getElementById("lot");
var fc = myNode.firstChild;
while( fc ) {
myNode.removeChild( fc );
fc = myNode.firstChild;
}
$(".toolbox-titlex").hide();
$(".panel").hide();
$("#container").removeClass("disabledbutton");
$("#toolbox").removeClass("disabledbutton");
}
我尝试在closeForm()
标记下的主JSP页面中添加<script>
方法,然后就可以了。但我的问题是,我已经在<script src="resources/js/taro/Customscripts/VisualEditor.js" type="text/javascript"></script>
下将该javascript文件作为<head>
引用到该JSP中。那么为什么不认识这种方法呢?
这方面的任何建议都将受到高度赞赏。
答案 0 :(得分:3)
尝试使用jQuery在ready函数中附加click事件,以确保在附加事件之前加载了所有代码:
$(function(){
$('#tableFormCancelButton').on('click', closeForm);
});
从按钮中删除内联事件onclick='closeForm()'
。
希望这有帮助。
答案 1 :(得分:0)
尝试将onclick='closeForm()'
更改为onclick='test()'
并在JSP添加
function test (){
closeForm();
};
希望这有帮助。