简体   繁体   English

单击按钮调用函数

[英]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.单击带有 id = tableList 的按钮时,我希望显示带有消息“这是数据”的警报,但它不显示任何内容。 I also tried with pasting the code related to click of button with id tableList between different tags, still it did not work.我还尝试在不同标签之间粘贴与单击带有 id tableList 的按钮相关的代码,但它仍然不起作用。 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.谢谢指点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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