【问题标题】:Redirect form to different URL based on select option element But not selected after redirect根据选择选项元素将表单重定向到不同的 URL 但重定向后未选择
【发布时间】:2015-03-02 11:18:39
【问题描述】:

如果除了 Option 之外选择了任何选项,我需要让表单重定向到另一个 url 然后显示选定的选项。

提前谢谢你

 <html> 
    <head>      
   <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>        
  </head>
   <body>    
        <form method='post'>   
            <select id="selectbox">  
                <option value="index.html" selected>Home</option>
                <option value="?hello=about" >Abut Us</option>
                <option value="?hello=contact" >Contact Us</option>
                <option value="?hello=blog">Blog</option>   
            </select>   
        </form>  

   <script>
        $(function(){
          // bind change event to select
          $('#selectbox').bind('change', function () {
              var url = $(this).val(); // get selected value
              if (url) { // require a URL
                  window.location = url; // redirect
              }
              return false;
          });
        });
    </script> 
    </body>  
    </html>

【问题讨论】:

  • 你的代码看起来不错,你有问题吗?
  • 当我选择任何选项时,我的代码重定向但不在重定向页面中显示选定的名称。始终显示“家”。
  • window.location.href = url;
  • 那是因为页面已经重新加载了。您需要将选择的值存储在某处,或从查询字符串中读取。

标签: javascript php jquery html ajax


【解决方案1】:

因此,假设您的网址具有查询字符串,例如“?hello=about”、“?hello=xxx”等。以下应该可以工作。在选择框绑定方法之后删除此代码。请记住,我没有测试过这段代码。

var option = location.search.replace(/(^\?hello=[^&#]+).*?$/i, "$1");
$('#selectbox').val(option);

【讨论】:

    【解决方案2】:

    随便用

    if (url) { // require a URL
        window.location.href = url; // redirect
    }
    

    因为在选项值中您没有传递完整的 URL。

    【讨论】:

    • 设置window.location 非常好。 OP已澄清问题是重定向后选择的值正在改变。
    • 好吧@罗里。对于 OP 的查询,我建议他不要重定向到另一个 url,只需将表单 Action 设置为 OP 在重定向后要显示的另一个文件,然后在 $_POST 下显示选定的值。我想它会起作用而不是重定向表单
    【解决方案3】:

    试试这个:

    $(function(){
        // bind change event to select
        $('#selectbox').bind('change', function () {
            var url = $(this).val(); // get selected value
            if (url) { // require a URL
                $( 'form' ).attr( 'action', url ).submit();
            }
            return false;
        });
    });
    

    【讨论】:

      【解决方案4】:

      我假设您希望表单比您的问题中指示的更多,否则这只是一个基本的跳转导航。如果您确实有更多数据要传输,则 window.location 不会从表单中发布该数据。您需要执行以下操作:

      $(function(){
          // bind change event to select
          $('#selectbox').bind('change', function () {
              var url = $(this).val(); // get selected value
              if (url) { 
                  // set the 'action' attribute to the
                  // value of the url var and submit the form.
                  $('form').attr('action', url).submit();
              }
              return false;
          });
      

      });

      【讨论】:

      • 我试过这个但没有用,当我选择任何选项时,我的代码重定向但不在重定向页面中显示选定的名称。始终显示“家”
      猜你喜欢
      • 2012-09-05
      • 2014-03-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-11-25
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多