How can I set location for a button on-click event

2020-05-06 11:29发布

问题:

I have this page:

link

Here on the homepage, you will find a button called "Get a quote". When you click on this button I want to open link "google.com".

I tried to do this with jQuery using this code but it's not working.

 jQuery(document).ready(function($) {
    $('.wpcf7-form-control wpcf7-submit').click(function() {
    window.location = "www.google.com/index.php?id=";
    });
});

This site is made with Wordpress and I used the plugin Contact Form 7

<div class="contact-frm">
        <div class="right">
            <?php echo do_shortcode('[contact-form-7 id="9" title="get a quote"]'); ?>
        </div>
    </div>

Can you please help me to solve this problem?

Thanks in advance!

回答1:

You forgot two things:

  1. Add a http:// in front of the URL.
  2. Change wpcf7-submit to .wpcf7-submit.

Use this way:

jQuery(document).ready(function($) {
    $('.wpcf7-form-control .wpcf7-submit').click(function() {
        window.location = "http://www.google.com/index.php?id=" + $(this).attr("id");
    });
});


回答2:

use the below code:

window.location = "http://www.google.com/index.php?id=";

adding http:// before the link can help you.

Also use:

$('.wpcf7-form-control .wpcf7-submit');

if wpcf7-submit is class specify it using dot . in jQuery

if wpcf7-submit is id then specify it using dot # in jQuery



回答3:

Instead of this :

jQuery(document).ready(function($) {
    $('.wpcf7-form-control wpcf7-submit').click(function() {
        window.location = "www.google.com/index.php?id=";
    });
});

Use this :

jQuery(document).ready(function($) {
    jQuery(function($) {
        $(".wpcf7-form-control.wpcf7-submit").click(function() {
            window.location = "http://www.google.com/index.php?id=";
        });
    });
});

It will work for you. There is something you should know:

  • In jquery selector you should write two classas without space
  • $ doesn't work in wordpress, For more information see here