簡體   English   中英

從動態創建的html表單按鈕調用Javascript函數

[英]Call a Javascript function from dynamically created html form button

我有一個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>"+
            "&nbsp<button id='tableFormConfirmButton' name='tableFormConfirmButton' class='btn btn-primary' onclick='storeTableFormInfo("+newAgent+","+i+","+e+","+mouseTop+","+mouseLeft+","+elemType+")'>Confirm</button>"+
            "&nbsp&nbsp<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");
}

我嘗試在JSP主頁中的<script>標記下添加closeForm()方法,然后它起作用了。 但是我的問題是,我已經在該JSP中將該JavaScript文件引用為<script src="resources/js/taro/Customscripts/VisualEditor.js" type="text/javascript"></script><head> 那么為什么不認識這種方法呢?

在這方面的任何建議將不勝感激。

嘗試使用jQuery將click事件附加到ready函數中,以確保在附加該事件之前已加載所有代碼:

$(function(){
    $('#tableFormCancelButton').on('click', closeForm);
});

並從按鈕中刪除內聯事件onclick='closeForm()'

希望這可以幫助。

嘗試將onclick='closeForm()'更改為onclick='test()'並在JSP上添加

function test (){
   closeForm();
};

希望這可以幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM