简体   繁体   English

当有人确认时,让 jQuery 单击按钮(对话框)

[英]Make jQuery click on a button when someone has confirmed (dialog box)

I have a wordpress auction site and wanted to add a confirm dialog with jquery when someone clicks on the "bid" button.我有一个 wordpress 拍卖网站,当有人点击“出价”按钮时,我想添加一个带有 jquery 的确认对话框。 I can achieve this with the default system dialog but I want a more custom dialog box with jQuery.我可以使用默认的系统对话框来实现这一点,但我想要一个带有 jQ​​uery 的自定义对话框。 How can I make it click the bid button once the end user has confirmed the bid?一旦最终用户确认出价,我如何让它点击出价按钮?

Here is the code example:下面是代码示例:

            <button type="submit" class="bid_button button alt"><?php echo wp_kses_post( apply_filters( 'bid_text', esc_html__( 'Bid', 'auctions-for-woocommerce' ), $product ) ); ?></button>
            <script>
                jQuery('button').confirm({
                    title: 'titletext',
                    content: 'content text"',
                    type: 'red',
                    buttons: {   
                        ok: {
                            text: "OK",
                            btnClass: 'btn-primary',
                            keys: ['enter'],
                            action: function(){
                                 console.log('Brukeren bekreftet "OK"');
                            }
                        },
                        No: function(){
                                text: "No",
                                jQuery.alert('Kansellert!');
                                console.log('the user clicked cancel');
                        }
                    }
                });
            </script>

it seems like this libary is not created for submitting forms via button, more like using it for <a> tags, source - https://github.com/craftpip/jquery-confirm/issues/229似乎这个库不是为通过按钮提交表单而创建的,更像是将它用于<a>标签,源 - https://github.com/craftpip/jquery-confirm/issues/229

I thought i will give you still a way to solve your problem.我想我会给你一个方法来解决你的问题。 now i am preventing the default behavior from the button when its not confirmed and trigger it again when its confirmed, but this time the button can submit the form.现在我正在阻止按钮未确认时的默认行为,并在确认时再次触发它,但这次按钮可以提交表单。 Also added the id submitButton to your button for making it individual. submitButton id submitButton添加到您的按钮以使其个性化。

<button type="submit" id="submitButton" class="bid_button button alt"></button>

<script>
   var confirmed = false;
   $('#submitButton').on('click', function() {
     if (!confirmed) {
       event.preventDefault();
       $.confirm({
         title: 'titletext ',
         content: 'content text"',
         type: 'red',
         buttons: {
           ok: {
             text: "OK",
             btnClass: 'btn-primary',
             keys: ['enter'],
             action: function() {
               confirmed = true;
               $('#submitButton').click()
               console.log('Brukeren bekreftet "OK"');
             }
           },
           No: function() {
             text: "No",
             jQuery.alert('Kansellert!');
             console.log('the user clicked cancel');
           }
         }
       })
     } else {
       confirmed = false
     }
   })
</script>

hope I could help you.希望我能帮助你。 :) :)

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

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