【问题标题】:on bind an input field that was generated using a ajax call绑定使用 ajax 调用生成的输入字段
【发布时间】:2018-10-09 08:01:37
【问题描述】:

我想将使用 ajax 生成的输入字段绑定到页面中。

我尝试了许多不同的 jQuery 代码,但都没有成功。

以下是我使用的代码:

$('#bookingcoupon').bind('input', function(){});
$(document).on('bind', '#bookingcoupon', function(){});
$('body').on('bind', '#bookingcoupon', function(){});
$('.divname').on('bind', '#bookingcoupon', function(){});

这些都不行

当我使用点击监听器时:

 $('.divwrapper').on('click', '.clickbutton', function(){});

这很好用,只是不喜欢 on() 函数的绑定版本。

任何建议都会被采纳:)

【问题讨论】:

  • 您应该使用on(),因此您使用的代码是正确的方法。 bind() 很久以前就被弃用了,不应该使用,也没有bind 事件。
  • 是的,根据上面的评论 on() 应该被使用。以下代码中的小修正:$(document).on('bind', '#bookingcoupon', function(){}); $('body').on('bind', '#bookingcoupon', function(){}); $('.divname').on('bind', '#bookingcoupon', function(){});。此处#bookingcoupon 将仅选择第一个具有 id 作为 bookingcoupon 的元素,而如果您使用 '[id="bookingcoupon"]' 那么它将选择所有具有 id 为 'bookingcoupon' 的元素。
  • @RoryMcCrossan 如您所见,我一直在使用 on 功能。它不起作用
  • 您的问题字面意思是“这很好用”...?你说它不工作?如果是这样,我们需要查看问题的完整示例,包括 HTML,以及更清晰地描述您期望发生的情况
  • @PrasadWargad 我已经尝试过 on() 函数,我的第一个想法是打开,但后来我尝试了 bind,因为我无法继续工作。我只使用了 id 名称,因为只有 1 个使用该 ID 生成的元素,否则我会完成 input[id=idname] :)

标签: jquery ajax jquery-on


【解决方案1】:

我做了这个例子,也许它可以帮助你:

http://jsfiddle.net/xpvt214o/873462/

// find elements
var button = $("button")

// handle click and add class
button.on("click", function(){
  $("#append").append( "<input class='sameClass' type='text'></input></br>" );

  $('.sameClass').change(function(){
    $('.sameClass').bind('change',functionChange(this));
  });

});

function functionChange(obj){
    alert($(obj).val());
};

还有 HTML:

<div id="banner-message">
  <button >Add TextBox</button>
  <div id="append">
  </div>
</div>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-12-06
    • 1970-01-01
    • 1970-01-01
    • 2016-06-20
    • 1970-01-01
    • 2017-08-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多