繁体   English   中英

按钮点击警报未触发

[英]on button click alert is not firing

我需要在 WooCommerce 下订单按钮上发出警报。 当我将代码放在下面的 id 为 'place_order' 时,它不起作用。 但是当我用任何其他元素 ID 更改此 ID 并单击它时,它就可以工作了!

当我单击以下按钮时,我需要此警报才能工作:

 jQuery(document).ready(function($) { $(function() { $('#place_order').click(function() { alert("This is a test message... please ignore this."); }); }); });
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <button type="submit" class="button alt" name="woocommerce_checkout_place_order" id="place_order" value="Place order" data-value="Place order">Pay securely with WorldPay</button>

你能告诉我我错过了什么吗? 是不是因为按钮调用了ajax? 如果是的话,我该如何进行这项工作?

谢谢

这是您如何对链接按钮或普通按钮执行此操作:

$(document).ready(function(){
    $('#place_order').click(function() {
        alert("This is a test message... please ignore this.");
    });
});

但是您使用的是提交按钮,因此表单已提交并且您看不到消息。 如果需要,这是一种停止表单提交的方法:

$(document).ready(function(){
    $('form').on('submit', function(event){
       event.preventDefault();
       event.stopPropagation();
       alert("Form Submission prevented/stopped.");
    });

    $('#place_order').click(function() {
        alert("This is a test message... please ignore this.");
    });
});

希望对你有帮助

你的代码一定是这样的,你可以检查你缺少什么。

<!DOCTYPE html>
<html>
        <link rel="stylesheet" type="text/css" href="jQuery_functions.css">
         <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>

<button type="submit" class="place_order" name="woocommerce_checkout_place_order" id="place_order" value="Place order" data-value="Place order">Pay securely with WorldPay</button>
<script type="text/javascript" src="js/jquery-1.7.1.min.js"></script>
<script type="text/javascript" >
$(document).ready(function(){
        $('#place_order').click(function(){ alert("This is a test message... please ignore this.");
    });
});
 </script>
</html>

希望我能帮上忙!

这是您如何对链接按钮或普通按钮执行此操作:

$(document).ready(function(){
    $('#place_order').click(function() {
        alert("This is a test message... please ignore this.");
    });
});

但是您使用的是提交按钮,因此表单已提交并且您看不到消息。 如果需要,这是一种停止表单提交的方法:

$(document).ready(function(){
    $('form').on('submit', function(event){
       event.preventDefault();
       event.stopPropagation();
       alert("Form Submission prevented/stopped.");
    });

    $('#place_order').click(function() {
        alert("This is a test message... please ignore this.");
    });
});

暂无
暂无

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

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