简体   繁体   中英

calling a function on click of a button

I have written the script as follows:

<script type="text/javascript">
    $(document).ready(function(){
        $("#Click").click(function(){
            var data1 = new Object();
            data1.name = $('#databases').val();
            $.ajax({
          url : "form_submit",
          type : 'POST',
          data : data1,
          dataType : "text",              
          success : function(data){
            data = JSON.parse(data);
            var output="<table id=tableStyle>";
            output+="<tr>" + "<th>" + "REPORTSUITE_ID" + "</th>" + "<th>" + "REPORTSUITE_NAME" + "</th>" + "<th>" + "STAGING_DATABASE" + "</th>" + "<th>" + "DWH_DATABASE" + "</th>" + "</tr>";
            for (var i in data)
            {
                output+="<tr>" + "<td>" + data[i].REPORTSUITE_ID + "</td>" + "<td>" + "<button type = \"buttion\" id =\"tableList\">" + data[i].REPORTSUITE_NAME + "</button>" + "</td>" + "<td>" + data[i].STAGING_DATABASE + "</td>" + "<td>" + data[i].DWH_DATABASE + "</td>" + "</tr>";
            }
            output += "</table>";
            document.getElementById("placeholder").innerHTML=output;
          }
          });
        });
        });
    $("#tableList").click(function(){
            alert("this is data");
            });
</script>

On the clicking of button with id = tableList, i want the alert with message "this is data" to be displayed but it does not display anything. I also tried with pasting the code related to click of button with id tableList between different tags, still it did not work. Please help

Hi used event delegation for the above question as pointed out as follows:

<script>
    $(document).on("ready",function(){
        $("#placeholder").on("click","button",function(event){
            alert("id is " + this.id + "for database " + $('#databases').val())
        })
    });
</script>

The value is being passed correctly. Thanks for the pointer.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM