在提交时使用JavaScript重定向表单插件

Use JavaScript to redirect form plugin on submit

本文关键字:重定向 表单 插件 JavaScript 提交      更新时间:2023-09-26

当用户点击提交时,我需要帮助尝试重定向wordpress表单插件。我可以将javascript添加到表单中,在那里我检查单选按钮的值并基于此重定向。下面是我试图添加的代码。下面是我在firebug中看到的错误。

Javascript:

on_sent_ok: "if (document.getElementByName('review_stars').value=='4')||(document.getElementByName('review_stars').value=='5')
{location = 'http://74.208.11.118:8085/review-submission/'} else { location = 'http://74.208.11.118:8085/review-thank-you/'};"
错误:

SyntaxError: unterminated string literal

"if (document.getElementByName('review_stars').value=='4')||

除非我瞎了,否则你会错过(and) if语句。试试吧:

on_sent_ok: "if (document.getElementByName('review_stars').value=='4' || document.getElementByName('review_stars').value=='5'){location = 'http://74.208.11.118:8085/review-submission/'} else { location = 'http://74.208.11.118:8085/review-thank-you/'};"

这是一个jQuery解决方案。

jQuery(document).ready(function($){
    var myform = $("#my_form_id");
    myform.submit(function(e){
        e.preventDefault();
        s = $("[name='review_stars']:checked");
          if(s.val() == 1) {
              window.location = "http://somedomain.com";
          } else if(s.val() == 2) {
              window.location = "http://anotherdomain.com";
          } 
    })
});