I am trying to get JQuery to work on dynamically loaded content through ajax with event binding, similar to this: Event binding on dynamically created elements?
(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");
});
And I tried to bind the events like this:
我试图绑定这样的事件:
(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);
And this is the main html page:
这是主要的html页面:
Content loaded:
1 个解决方案
#1
This seems to be solved with $(document).ajaxComplete (function (){ and just duplicating the original code. Thanks for the help everyone!
在 Windows 10 环境中,通过配置 Visual Studio Code (VSCode) 实现基于 Windows Subsystem for Linux (WSL) 的 C++ 开发,并启用智能代码提示功能。具体步骤包括安装 VSCode 及其相关插件,如 CCIntelliSense、TabNine 和 BracketPairColorizer,确保在 WSL 中顺利进行开发工作。此外,还详细介绍了如何在 Windows 10 中启用和配置 WSL,以实现无缝的跨平台开发体验。 ...
[详细]