WordPress联系表格7重定向单选按钮值不起作用

Wordpress Contact Form 7 Redirect on radio button value not working

本文关键字:单选按钮 不起作用 重定向 联系 表格 WordPress      更新时间:2023-09-26

在过去的 7 小时内,我一直在研究基于值的联系表单 5 重定向。 我无法解释为什么我的代码不起作用。 有人能看到问题吗?

联系表格单选按钮

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

其他设置

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

当我选择单选按钮 9 时,我被重定向到 msn(else 参数)。 所有其他值也归 MSN(至少该部分是正确的)。 我尝试将其他设置更改为三等号 ===,但这没有帮助。 我还尝试在单选按钮中的每个值后添加一个管道 (|)。 那也没用。

看看生成的 HTML,你就会知道为什么你的代码不能按预期工作。有2个选项

艰难的方式

在外部JS文件中定义一个函数,该函数循环访问单选按钮

function getRating(radioName) {
  var ratings = document.getElementsByName( radioName );
  for( var r in ratings ) {
    if( ratings[r].checked )
      return ratings[r].value;
  }
  
  return null;
}

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

简单的方法:-)

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

这是非常简单的方法

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