简体   繁体   English

在页面加载后加载的元素上使用jquery

[英]Use jquery on element that is loaded after pageload

I want to check if a button is enabled or disabled, but the button is added after pageload. 我想检查某个按钮是启用还是禁用,但是该按钮是在页面加载后添加的。 How can I do this? 我怎样才能做到这一点?

My code needs to show a message when the button is disabled and remove the message when the code is enabled. 禁用按钮时,我的代码需要显示一条消息,而启用代码时,则需要删除消息。

My code: 我的代码:

jQuery(document).ready(function() {
    "use strict";

if(jQuery('#checkoutbtn').prop(':disabled')){
    console.log('disabled');
    jQuery("#voorwaarden").css("display", "inline-block");
}else{
    console.log('enabled');
    jQuery("#voorwaarden").css("display", "none");
}
});

My html code: 我的html代码:

<div class="checkoutvoorwaarden" id="voorwaarden">Ga akkoord met onderstaande voorwaarden om door te gaan:</div>
<button type="submit" title="<?php echo $this->__('Place Order') ?>" disabled="disabled" class="button btn-checkout" id="checkoutbtn" onclick="review.save();"><span><?php echo $this->__('Place Order') ?></span></button>
<p><input type="checkbox" class="checkbox chk" id="voorwaarden" style="display:inline;" required/><b> Ik ga akkoord met de algemene voorwaarden <em style="color:#ff2727;">*</em></b></p>
<p><input type="checkbox" class="checkbox chk" id="geboorte" style="display:inline;"/><b> Ik ben ouder dan 18 jaar <em style="color:#ff2727;">*</em></b></p>

Part of my jquery that only works when written like this (wrapped inside a document): 我的jquery的一部分仅在像这样写(包装在文档中)时才起作用:

jQuery(document).on('change','.chk', function() {
   var checkCount=jQuery('.chk:checked').length;
   var totalCheckbox =jQuery('.chk').length;
   totalCheckbox==checkCount ? jQuery("#checkoutbtn").prop('disabled', false):jQuery("#checkoutbtn").prop('disabled', true);
  });

But I don't know how to use this .on('change') for the .is(:disabled) property. 但是我不知道如何为.on('change') .is(:disabled)属性使用此.on('change')

If you want to interact on an element, you need to run the code once the element has been added. 如果要在元素上进行交互,则在添加元素后需要运行代码。

If it is coming from an Ajax request, you could use the success handler. 如果它来自Ajax请求,则可以使用成功处理程序。

If it's a function that adds the element, call your method after it has done the work. 如果它是添加元素的函数,请在完成工作后调用您的方法。

You could create an event handler with your code and trigger that event every time it is needed. 您可以使用代码创建事件处理程序,并在每次需要时触发该事件。

If all that fails for X or Y reason, you could also poll the document using an interval to be able to act once the element has been added. 如果由于X或Y原因所有操作均失败,则还可以使用间隔来轮询文档,以便在添加元素后立即采取行动。

var interval = window.setInterval(function(){
    if(jQuery('#checkoutbtn').length){
        //Check if element exists.
        window.clearInterval(interval);//Make sure we stop.
        if(jQuery('#checkoutbtn').is(':disabled')){
            console.log('disabled');
            jQuery("#voorwaarden").css("display", "inline-block");
        }else{
            console.log('enabled');
            jQuery("#voorwaarden").css("display", "none");
        }
    }
},1000);

Note that this is a last resort. 请注意,这是不得已的方法。 You should have plenty other possibilities using the first 3 propositions. 使用前三个命题,您应该有很多其他可能性。

You could wrap your code in document.ready .It will perform after the button created that means full document loaded 您可以将代码包装在document.ready它将在创建按钮后执行,这意味着已加载完整文档

$(document).ready(function(){
if(jQuery('#checkoutbtn').is(':disabled')){
    console.log('disabled');
    jQuery("#voorwaarden").css("display", "inline-block");
}else{
    console.log('enabled');
    jQuery("#voorwaarden").css("display", "none");
}
})

Snippet 片段

 $(document).ready(function() { if (jQuery('#checkoutbtn').is(':disabled')) { console.log('disabled'); jQuery("#voorwaarden").css("display", "inline-block"); } else { console.log('enabled'); jQuery("#voorwaarden").css("display", "none"); } }) 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="checkoutvoorwaarden" id="voorwaarden">Ga akkoord met onderstaande voorwaarden om door te gaan:</div> <button type="submit" title="<?php echo $this->__('Place Order') ?>" disabled="disabled" class="button btn-checkout" id="checkoutbtn" onclick="review.save();"><span><?php echo $this->__('Place Order') ?></span></button> <p><input type="checkbox" class="checkbox chk" id="voorwaarden" style="display:inline;" required/><b> Ik ga akkoord met de algemene voorwaarden <em style="color:#ff2727;">*</em></b></p> <p><input type="checkbox" class="checkbox chk" id="geboorte" style="display:inline;" /><b> Ik ben ouder dan 18 jaar <em style="color:#ff2727;">*</em></b></p> 

You need to check whether your button element is exists or not like below: 您需要检查按钮元素是否存在,如下所示:

This will check first button is loaded or not if it is loaded then only your functionality will work. 这将检查第一个按钮是否已加载(如果已加载),那么只有您的功能可用。

if ( $( "#checkoutbtn" ).length ) {

   if(jQuery('#checkoutbtn').prop(':disabled')){
    console.log('disabled');
    jQuery("#voorwaarden").css("display", "inline-block");
}else{
    console.log('enabled');
    jQuery("#voorwaarden").css("display", "none");
}

}

JQUERY Official Reference: https://learn.jquery.com/using-jquery-core/faq/how-do-i-test-whether-an-element-exists/ JQUERY官方参考: https : //learn.jquery.com/using-jquery-core/faq/how-do-i-test-whether-an-element-exists/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM