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

2020-05-06 11:11发布

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!

3条回答
够拽才男人
2楼-- · 2020-05-06 11:26

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楼-- · 2020-05-06 11:36

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
查看更多
爱情/是我丢掉的垃圾
4楼-- · 2020-05-06 11:43

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");
    });
});
查看更多
登录 后发表回答