Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/449.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何从动态按钮获取id?(jQuery)_Javascript_Php_Jquery - Fatal编程技术网

Javascript 如何从动态按钮获取id?(jQuery)

Javascript 如何从动态按钮获取id?(jQuery),javascript,php,jquery,Javascript,Php,Jquery,我使用jQuery和PDO来动态分页 当我创建一些具有id=xx的按钮时 然后使用jQuery尝试获取一个按钮id,它不起作用 这是我的代码: $records = $databaseConnection->query("SELECT * FROM area ORDER BY FIELD(local,'Earth','Mars','Mercury','Sun')LIMIT $start, $records_per_page"); $count = $records->

我使用
jQuery
PDO
来动态分页

当我创建一些具有
id=xx的
按钮时

然后使用jQuery尝试获取一个
按钮
id,它不起作用

这是我的代码:

$records    = $databaseConnection->query("SELECT * FROM area ORDER BY FIELD(local,'Earth','Mars','Mercury','Sun')LIMIT $start, $records_per_page");
$count      = $records->rowCount();
$HTML='<table border="1"><tr><th>local</th><th>target</th><th>detail</th></tr>';
if($count > 0)
{
    foreach($records as $row) {
        $HTML.='<tr>';
        $HTML.='<td>'.$row['local'].'</td>';
        $HTML.='<td>'.$row['target'].'</td>';
        $HTML.='<td><button class = click id ='.$row['id']. '>detail</button></td>';
        $HTML.='</tr>';

    }
$HTML.='</table>';
}

else
{
    $HTML='No Data Found';
}
echo $HTML;
echo $pagination;
如果我输入代码:

<html>
    <head>
        <title> Planet </title>
        <meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
        <script type="text/javascript" src="library/jQuery/jquery-1.11.3.min.js"></script>
        <script type="text/javascript">
            $(document).ready(function(){
                $("button").click(function(){
                    alert(this.id);
                });
            });
        </script>
    </head>
    <body>

    (dynamic of pagination code)
    ...
    ...
    ...

    <button id = "1"> try </button>
    </body>
</html>

行星
$(文档).ready(函数(){
$(“按钮”)。单击(函数(){
警报(this.id);
});
});
(动态分页代码)
...
...
...
尝试
当我单击id=1的
按钮
时,它就工作了

但动态按钮不起作用


我的代码中有什么内容???

您必须使用
绑定单击处理程序。on()
有关动态创建的元素,请参见下面的代码

$(document).ready(function(){
  $(document).on("click","button",function(){
    alert(this.id);
  });
});

很抱歉,我刚才看到你在谈论动态添加按钮。 我确信委托事件处理程序将有助于:

问题解释:

动态附加html时,新创建的DOM对象不会附加事件处理程序,因为附加处理程序的代码已经运行。

使用委托事件处理程序,可以将事件处理程序附加到运行代码时存在的父元素(本例中为文档)。单击子元素(无事件处理程序)时,浏览器会弹出事件并由其父元素处理(附加事件处理程序)

范例

$(document).on('click',".close_fast",function(event){
     alert("hi");
 });
你的密码是:

$HTML.='<td><button class = click id ='.$row['id']. '>詳情</button></td>';
$HTML.='詳情';
这不应该是:

$HTML.='<td><button class="click" id="'.$row['id']. '">詳情</button></td>';
$HTML.='詳情';

否则HTML无效。

必须使用
$(文档)。在(“单击”,“按钮”,函数(){//do})
上,我需要花时间阅读
上()
事件…不应该是“警报($(this).attr('id');?正常
。单击()
处理程序可以附加到页面中已经存在的元素,但是,对于动态创建的元素,我们需要将单击处理程序附加到
文档
对象,然后将其调用委托给
按钮
元素
.on()
单击页面时,将事件委托给匹配的选择器。浏览我提供的更多信息链接。好的,我修改这个错误,非常感谢!!让我试着这样说:我的原始代码没有被处理,动态代码create按钮无法获取,rigth~?
$HTML.='<td><button class="click" id="'.$row['id']. '">詳情</button></td>';