簡體   English   中英

如何通過單擊一個按鈕在同一選項卡上打開一個新網頁?

[英]how to open a new web page on the same tab by clicking a button?

我是網頁上的一個按鈕。 它的代碼是:

/* code of the other forms  */
<form method="get"  onsubmit="return validation()" > 
     <input type="submit" id="x" value="Search"  onmouseover="mover(this)" onmouseout="mout(this)" /> 
</form>

當我單擊該按鈕時,應驗證其他表單的值並在同一選項卡上打開新的網頁。 驗證部分沒有問題。 問題是新網頁在新標簽頁中打開,這不是我想要的。 到目前為止,代碼是:

 function validation(){
    var x=document.forms["flyrics"]["lyrics"].value;
    if (x==null || x=="")
    {
        alert("Search without Lyrics?");
        return false;
    }

    var y=document.forms["fartist"]["artist"].value;
    if (y==null || y=="")
    {
        alert("Search without Artist Name?");
        return false;
    }

            window.open("songList.html", "_self");      
} 

在最后一行中,它是在同一選項卡上打開新頁面的代碼。 但是每次我單擊按鈕時,都會打開一個新標簽。 我該如何糾正? 我也嘗試使用以下代碼。 但我不知道為什么他們都不在同一個標​​簽上打開新頁面。

 location.href = "songList";
 window.location="songList.html";
 window.open("songList.html",  "currentWindow", "");

實際問題在哪里? 需要幫助修復它。 我只需要使用javascript和html來做到這一點。

如果你只是這樣做

        window.open("songList.html");  

嘗試使用window.location.assign

window.location.assign("songList.html");

嘗試這個

window.location.replace("songList.html");

這對我來說很好!

我認為您想要實現的是在提交表單之前先對搜索進行驗證? 您必須在表單選項卡中添加action屬性,然后將return validation()移至“提交”按鈕。

看下面我的例子:

這將允許您在同一選項卡上加載songList.html

 function validation() { var x=document.forms["flyrics"]["lyrics"].value; if (x==null || x=="") { alert("Search without Lyrics?"); return false; } var y=document.forms["fartist"]["artist"].value; if (y==null || y=="") { alert("Search without Artist Name?"); return false; } } 
 <!DOCTYPE html> <body> <form id="flyrics"> <input type="text" id="lyrics" value=""> </form> <form id="fartist"> <input type="text" id="artist" value=""> </form> /* code of the other forms */ <form id="songList" action="songList.html"> <input type="submit" id="x" value="Search" onclick="return validation()"/> </form> </body> 

暫無
暫無

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

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