簡體   English   中英

Javascript-從當前網址中刪除查詢字符串

[英]Javascript - Remove query string from current url

單擊提交按鈕后,我需要從網址中刪除查詢字符串值。 我可以用jQuery做到這一點嗎?

當前網址:

siteUrl/page.php?key=value

頁面提交后:

siteUrl/page.php

實際上,我已經從另一個帶有查詢字符串的頁面着陸到當前頁面。 我需要在頁面首次加載時查詢字符串值以預填充一些詳細信息。 但是,一旦我提交了表格,我就需要刪除查詢字符串值。

我已經嘗試過了

$('#submit').click(function(){
var newUrl = window.location.href.replace(window.location.search,'');
window.location.href  = newUrl;
return false;
});

可以按預期更改url。 但無法獲取發布的值。

提前致謝 :)

這個怎么樣。 希望能幫助到你 :)

$('#myform').submit(function(event){
event.preventDefault();
var currentURL = window.location.href ;
location.href = currentURL.substring(0, currentURL.indexOf('?'));
});

index.html

<form id = "myform">
<input type = "text">
<input type = "submit" id = "submit" value = "Send">
</form>
function getQueryString(url) {
  return url.split("?")[0];
}
var Url=getQueryString("siteUrl/page.php?key=value");
console.log(Url);

您可以嘗試獲取網址

不幸的是我不能用javascript或jquery做到這一點。 所以我經歷了PHP重定向,現在可以工作了。

<?php
if(isset($_POST['Submit'])) {
    // actions
    if(isset($_REQUEST['key']) && ($_REQUEST['key'] != "")) {
        header('Refresh: 1;url='.$_SERVER['PHP_SELF']);
    }
}
?>

謝謝大家:)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM