如何使用 ajax 重定向到另一个具有隐藏值的页面


How to redirect to another page with a hidden value using ajax

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

表单帖子不起作用,因为页面链接正在修改,例如#!/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并在按钮 clik 上重定向。

$.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' 
});

不确定你想要实现什么,但希望上面的这段代码能帮助你。

$('#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");   
   }
}