weixin_33701251 2016-07-21 16:20 采纳率: 0%
浏览 28

Ajax的把手按钮ID

Imagine I have a handlebar template like this:

       {{#each this}}
           {{username}}
           <form method="post" id="form-id-{{tirID}}">
              <input type="button" id="btn-id-{{tirID}}">
           </form>
       {{/each}}

This one loops through data and works great with AJAX get method. However I would like to also use the button inside this template. I can give it a ID like I do above in the template. But how can I target the specific button to activate a AJAX post method ? The following wont work because I am not operating in the template anymore so {{ x }} can not be found:

       $('#btn-id-{{tirID}}').on('click', '.mydiv', function(event) {

           $.ajax({
                 type:'POST',
                 url: 'and so on....

       } 

Hope someone have a suggestion!

  • 写回答

1条回答 默认 最新

  • weixin_33737134 2016-07-21 16:55
    关注

    You could do something like this:

    {{#each this}}
        {{username}}
        <form method="post" id="form-id-{{tirID}}">
           <input type="button" id="btn-id-{{tirID}}" class="js-btn" data-id="{{tirID}}">
        </form>
    {{/each}}
    

    Then listen to js-btn and use the data property to get the id.

    $('.js-btn').on('click', '.mydiv', function(event) {
           var id = $(this).data().id;
           $.ajax({
                 type:'POST',
                 url: 'and so on....
    
       } 
    
    评论

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器