在此代碼中 document.location 回傳為 'Content/Search/(the searchboxID's value)' 。但是,當我這次嘗試在同一頁面中進行另一次搜索時,document.location 回傳為 Content/Search/(the searchboxID's first value)/(the searchboxID's new value) 。每次搜索并添加 $(searchboxID).val() 作為 Url.Action 中的路由引數時,如何洗掉第一個值?
<input type="text" id="txtSearch" class="tftextinput2" name="searchKey" size="21" maxlength="120" value="">
<input type="button" value=">" onclick='redirectOnClick()' class="tfbutton2" id="srcButton">
<script type="text/javascript">
console.log("result:" document.getElementById('txtSearch').value)
$(document).ready(function () {
var test =document.getElementById('srcButton').addEventListener("click",redirectOnClick);
var searchboxID = "#txtSearch";
function redirectOnClick() {
var routeLink = document.getElementById('txtSearch').value;
console.log("route " routeLink)
document.location = '@Url.Action("Search","Content")' '/' $(searchboxID).val();
console.log(document.location.pathname);
$(searchboxID).empty();
//location.reload();
}
});
</script>
uj5u.com熱心網友回復:
但是網址是
Content/Search/(the searchboxID's first value)/(the searchboxID's new value)
這是使用@Url.Action框架將從當前模型的屬性中提供任何缺失引數的常見問題。
給定一個字串引數searchTerm,
@Url.Action("Search", "Content")
變得一樣
@Url.Action("Search", "Content", new { searchTerm = Model.SearchTerm })
因此,您需要顯式地為該引數提供一個空白值,以便它不會被渲染:
@Url.Action("Search", "Content", new { searchTerm = "" })
(或= string.Empty)
為新值添加原始 javascript 組件給出:
url = '@Url.Action("Search","Content", new { searchTerm = "" })' '/' $(searchboxID).val();
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/526330.html
標籤:javascriptjQueryasp.net-mvc
下一篇:Phaser3補間不啟動
