简体   繁体   中英

Wordpress Contact Form 7 Redirect on radio button value not working

I've been researching Contact Form 7 redirects based on values for the past 5 hours. I cannot explain why my code doesn't work. Can anyone see a problem?

Contact Form Radio Button

[radio radio-935 id:rating label_first "1" "2" "3" "4" "5" "6" "7" "8" "9" "10"]

Additional Settings

on_sent_ok: " if (document.getElementById('rating').value=='9') {location.replace('http://www.google.com')} else { location.replace('http://www.msn.com/') } "

When I select radio button 9, I am redirected to msn (the else argument). All other values also go to MSN (at least that part is right). I've tried changing the additional settings to a triple equals sign === but that didn't help. I've also tried adding a pipe (|) after each value in the Radio Button. That didn't work either.

Have a look at the generated HTML and you'll know why your code doesn't work as expected. There are 2 options

The hard way

Define a function in a external JS file which iterates through the radio buttons

function getRating(radioName) {
  var ratings = document.getElementsByName( radioName );

  for( var r in ratings ) {
    if( ratings[r].checked )
      return ratings[r].value;
  }

  return null;
}

and then on_sent_ok: if( getRating( "radio-935 ") == 9 ) { location.replace('http://www.google.com') } else { location.replace('http://www.msn.com') }

The easy way :-)

on_sent_ok: if( $( "input[name='radio-935']:checked" ).val() == 9 ) { location.replace('http://www.google.com') } else { location.replace('http://www.msn.com') }

This one is very easy way

on_sent_ok: "var sol = $("input[name=radio-935]:checked").val();if(sol == '9'){ location = ' http://google.com '; } else { location = ' http://msn.com '; }"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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