首页 > 解决方案 > 包含值时如何在 window.open 中使用 _self?

问题描述

$("#input").keypress(function(event) {
        if (event.keyCode == 13) {
            window.open("search.html?search=" + $("#input").val()); // here is the location
            let searchString = $("#input").val()
            localStorage.setItem("search", JSON.stringify(searchString)); 
        }
    });

它的工作,但打开另一个选项卡。没有 + $("#input").val());

尝试使用 window.location 和 SO 上的所有其他示例...请帮助

标签: jqueryselfwindow.open

解决方案


你能试试这个:

<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
 $("#input").keypress(function(event) {
        if (event.keyCode == 13) {
            window.location = "search.html?search=" + $("#input").val(); // here is the location
            let searchString = $("#input").val()
            localStorage.setItem("search", JSON.stringify(searchString)); 
        }
    });
});
</script>
</head>
<body>

<input id="input"/>
</body>
</html>

推荐阅读