动态按钮不调用javascript函数?

时间:2022-12-05 11:58:00

I have a button wich im dynamically adding to my page like this.

我有一个按钮,我可以像这样动态添加到我的页面。

if (adminInd  =='X'){

    var adminDiv = $(
        '<label id=Delegatelbl>Miscellaneous label prototyping.:</label>'+
        '</br></br>'+
         '<div style="padding-right:152px;"> '+
            '<form action="userfop.jsp" id="userForm" >'+
              '<label for="tuid">TU-ID:</label>'+
              '<input type="text" name="tuid" id="tuid" maxlength="9"/>'+
            '<button type="button" id="RoleChanger" style="cursor:pointer" class="ab_submit" onclick="validateTuid()"> Change Role </button>'+
            '</form>'+
         '</div>');

    adminDiv.insertBefore('#FormDiv');


    } 

as you can see from the button tag im trying to call a javascript function and i just about tried everything and the function does not trigger. What im i doing wrong. I've been trouble shooting this for 2 hours now with no success. here is the function I dont see what I'm doing wrong. Someone please point me in the right direction.

正如你可以看到我试图调用一个javascript函数的按钮标签,我只是尝试了一切,功能不会触发。我做错了什么我一直在拍摄这个2小时的麻烦,但没有成功。这是我看不出我做错了什么的功能。有人请指出我正确的方向。

function validateTuid(){

    var textTuid = document.getElementById(tuid).value  

    $.ajax({  type: "POST",
          url: "dbfunctions.jsp",  
          data: {TYPE:"V", tuidval:textTuid }}).done(function( msg )
           {  
                alert("Data Saved: " + $.trim(msg));
            if (msg != null || msg != ''){userForm.submit()}
                return "N";
          });

2 个解决方案

#1


5  

You should delegate the click event, you can use on() method, try the following:

您应该委派click事件,您可以使用on()方法,尝试以下方法:

$(document).on('click', "#RoleChanger", validateTuid)

#2


0  

Maybe have a look at http://api.jquery.com/live/. Also, instead of hardcoding onClick in the button bind a listener to it instead.

也许看看http://api.jquery.com/live/。此外,代替按钮中的onClick硬编码而不是将侦听器绑定到它。

#1


5  

You should delegate the click event, you can use on() method, try the following:

您应该委派click事件,您可以使用on()方法,尝试以下方法:

$(document).on('click', "#RoleChanger", validateTuid)

#2


0  

Maybe have a look at http://api.jquery.com/live/. Also, instead of hardcoding onClick in the button bind a listener to it instead.

也许看看http://api.jquery.com/live/。此外,代替按钮中的onClick硬编码而不是将侦听器绑定到它。