【问题标题】:How to redirect to another page with a hidden value using ajax如何使用ajax重定向到具有隐藏值的另一个页面
【发布时间】:2013-10-31 18:54:53
【问题描述】:

在将隐藏值传递给该页面时,我必须重定向到另一个页面。

由于页面链接正在修改,例如 #!/newpage.php 等,表单帖子无法正常工作。

 <form id="form1" name="form1" method="post" action="newpage.php">
    <input type="hidden" name="pid" id="pid" value="<?php echo $id; ?>" />
    <input type="submit" name="button" id="button" value="Send ID" />
  </form> 

相反,我想使用类似于以下的代码。

  $(document).ready(function()
   {
           $("#sendid").click(function()
            {
                 if($("#pid").val().length == 0)
                 {
                      alert("Error!");
                 }
                 else
                 {
                      $.post("newpage.php",
                      { 
                           pid:$("#pid").val(),
                           window.location.href("#!/newpage.php");
                      }
               }
          }
    });

所以基本上我们需要将隐藏值发送到newpage.php 并在按钮点击时重定向。

【问题讨论】:

    标签: javascript php jquery ajax redirect


    【解决方案1】:
    $.ajax({   
        type: "POST",   
        url: 'newpage.php',   
        data: {pid:$("#pid").val()},   
        always: function( data ) {
           window.location.href("#!/newpage.php");
           //or if you want to submit form
           $("#form1").submit();   
        },
        dataType: 'json' 
    });
    

    不确定你想要达到什么目标,但希望上面的代码对你有所帮助。

    【讨论】:

    • 为什么是 json 数据类型?通常,如果您不确定他们想要什么,那么回答这个问题可能不是最好的主意。您应该知道代码是正确的,而不仅仅是希望。
    • 您好,感谢您的回复。但它不起作用,或者我无法正确实施它。能不能再看一遍。谢谢!
    【解决方案2】:
    $('#button').submit(sendid);
    function sendid()
    {
        $.ajax( {
       url: 'newpage.php',
       type: "post",
       data:  {"pid":$("#pid").val()},
       cache: false,
       success: function(response){
             window.location.href("#!/newpage.php");   
       }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-01-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-03-12
      • 2011-02-22
      • 2011-06-19
      相关资源
      最近更新 更多