【问题标题】:JQuery Event binding of Ajax Loaded contentAjax 加载内容的 JQuery 事件绑定
【发布时间】:2015-05-13 05:58:27
【问题描述】:

我正在尝试让 JQuery 通过带有事件绑定的 ajax 处理动态加载的内容,类似于:Event binding on dynamically created elements?

但是,我对 java/Jquery 不太擅长,而且我似乎无法让 jquery 处理 ajax 内容。我从这段代码开始:

(function($){
function floatLabel(inputType){
        $(inputType).each(function(){
            var $this = $(this);
            var text_value = $(this).val();

            // on focus add class "active" to label
            $this.focus(function(){
                $this.next().addClass("active");
            });

            // on blur check field and remove class if needed
            $this.blur(function(){
                if($this.val() === '' || $this.val() === 'blank'){
                    $this.next().removeClass();
                    }
            });

            // Check input values on postback and add class "active" if value exists
            if(text_value!==''){
                $this.next().addClass("active");
                }
            });
    // Automatically remove floatLabel class from select input on load
    //$( "select" ).next().removeClass();
}
// Add a class of "floatLabel" to the input field
floatLabel(".floatLabel");
});

我尝试像这样绑定事件:

(function($){
    var $this = $('.floatLabel');
    var text_value = $('.floatLabel').val();
$('.container').on('focus', '.floatLabel', function floatLabel(inputType){
    $(inputType).each(function(){
        $this.next().addClass('active');
        if(text_value!==''){
            $this.next().addClass('active');
        }
    });
});
$('.container').on('blur', '.floatLabel', function floatLabel(inputType){
        $(inputType).each(function(){
            if($this.val() === '' || $this.val() === 'blank'){
                $this.next().removeClass();
        }
        });
    });
})(jQuery);

这是主要的html页面:

<div class="container">
   <div id="ha">
     <script type="text/javascript">
      $( document ).ready(function() {
      $('#ha').load('example.com/createform');
});
     </script>
   </div>
</div>

已加载内容:

<div >
 <input class="floatLabel" name="example">
 <label for="example">example</label>
</div>

【问题讨论】:

    标签: javascript jquery ajax


    【解决方案1】:

    这似乎可以通过$(document).ajaxComplete (function (){ 解决,只需复制原始代码即可。感谢大家的帮助!

    【讨论】:

      猜你喜欢
      • 2012-03-24
      • 2011-09-28
      • 2023-04-04
      • 2013-05-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-05-08
      相关资源
      最近更新 更多