Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/79.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
如何在jquery模板中为每个循环添加按钮_Jquery - Fatal编程技术网

如何在jquery模板中为每个循环添加按钮

如何在jquery模板中为每个循环添加按钮,jquery,Jquery,当我单击此MoreInformation按钮时,我没有收到任何警报。ID必须是唯一的,因此如果在循环中多次使用它,将导致问题。它应该是一个类 如果将其更改为class=btncabinmoreinfo1,然后单击事件 $(“.btncabinmoreinfo1”)。单击(函数(){ 警报(“更多信息”) 你可能会更幸运。你做错了。你没有用引号表示输入按钮的id和值 $("#btncabinmoreinfo1").click(function () { alert("more info")

当我单击此MoreInformation按钮时,我没有收到任何警报。

ID必须是唯一的,因此如果在循环中多次使用它,将导致问题。它应该是一个类

如果将其更改为class=btncabinmoreinfo1,然后单击事件

$(“.btncabinmoreinfo1”)。单击(函数(){ 警报(“更多信息”)


你可能会更幸运。

你做错了。你没有用引号表示输入按钮的id和值

$("#btncabinmoreinfo1").click(function () {
    alert("more info");
});

我认为你在做这件事

$("#btncabinmoreinfo1").click(function () {
 alert("//something");
})
然后

$.tmpl( "cabinTemplate", data).appendTo( "#elementid" );
所以,你可以做一些改变,比如

  • 用引号将属性值括起来
  • 因为你们是在循环中做的,所以在你们的按钮上添加一个类
  • 将事件附加到模板上,如父级

  • var markup=“${hotelname}
    ";
    $.template(“cabinTemplate”,标记);
    $(“#elementid”)。在('click','.btncabinmoreinfo1',(函数(){
    警报(“更多信息”);
    });
    
    你可以在这里查一查

    委派事件方法仅将事件处理程序附加到一个元素上,即
    #elementid
    ,事件只需冒泡(从单击的
    .btncabinmoreinfo1
    #elementid
    ):

    因此,通过单击
    ,事件将附加到
    #elementid
    元素。btncabinmoreinfo1
    将弹出气泡,因此新的动态元素也可以响应事件


    我希望,这会有所帮助:)

    而不是id!!我应该使用class?如果我正确理解了你的问题,并且这个按钮正在循环中使用,那么它应该是class,因为id不会是唯一的。是的,这也是一点,id应该是唯一的。即使我也尝试过class。但是当我单击时,该按钮没有响应。即使在控制台中,它也不会显示任何内容错误。您是否也更改了click函数以响应非id类,如:$(“.btncabinmoreinfo1”)。单击(函数(){alert(“more info”);});是的,我已更改。它不工作。使用.on()它工作。当我在循环中保留引号时,它显示错误,如(;预期)“on”代替“click”有什么用?hotelname是数据库中特定表的文件名
    $("#btncabinmoreinfo1").click(function () {
     alert("//something");
    })
    
    var markup = "<tr><td colspan='2'><h1>${hotelname}</h1></td></tr> 
    <tr><td><input type=button value=MoreInformation id=btncabinmoreinfo1></td></tr>";
        $.template("cabinTemplate", markup);
    
    $.tmpl( "cabinTemplate", data).appendTo( "#elementid" );
    
    $("#btncabinmoreinfo1").click(function () {
        alert("more info");
    });
    
    var markup = "<tr><td colspan='2'><h1>${hotelname}</h1></td></tr> 
    <tr><td><input type='button' value='MoreInformation' class='btncabinmoreinfo1'></td></tr>";
    $.template("cabinTemplate", markup);
    
    $("#elementid").on('click' , '.btncabinmoreinfo1',(function () {
        alert("more info");
    });