2013-08-28 20 views
0

如何在javascript上編寫此代碼。 這是在html表單上。在javascript中的url操作

<form action="@Url.Action("NewPage")" > 
.... 
</form> 

現在我已經有了javascript的功能。

function validateForm() { 
    //var x = document.forms["form"]["fname"].value; 
    var x = document.getElementById('id').value; 
    if (x == null || x == 0 || x == "0") { 
     alert("stop"); 
     return false; 
    } 
    else { 
     document.form.submit(); 


    } 
} 

什麼應該在html表單動作。

+3

用JavaScript寫的麼?你在問什麼? –

+0

Click event for page。通常這用於導航到新的html頁面。請提供您的要求的更多細節。 – Akki619

+3

你好,asp.net MVC ...你在那裏,沒有被標記? –

回答

0

編輯:它現在看起來像你想讓你的HTML表單重定向到提交的另一頁。爲了實現這個目標,這樣做:

<form action="newurl.php" method="post"> 
[...] 
</form> 

其中newurl.php是要提交到任何網頁。


原來的答案:

如果你想使用JavaScript來更改URL,這是什麼樣子,你想這樣做,這樣做:

<script type="text/javascript"> 
    location.href="http://new.url/whatever"; 
</script> 

如果你想在按鈕上按下它,將其包裝在一個功能中並讓按鈕調用該功能:

<script type="text/javascript"> 
function newURL(url) { 
    location.href=url; 
} 
</script> 
<input type='button' value='Google' onclick="newURL('http://google.com')" /> 
0

對於紅色irection,使用這些

- alert(document.URL) 

    - alert(Window.location.href) 

    - alert(document.location.href) 

所以重定向,使用此功能,並適當地調用它:

function validateForm() { 
//var x = document.forms["form"]["fname"].value; 
var x = document.getElementById('id').value; 
if (x == null || x == 0 || x == "0") { 
    alert("stop"); 
    return false; 
} 
else { 
    document.form.submit(); 
    Window.location.href="www.google.com";//redirect on form submit 
} 

}

注意:如果你有一個動作頁,然後給它一個頭。

+0

@Nejc Galof:plz接受答案如果你認爲它值得 –

0

試試這個

action='@Url.Action("ActionName","ControllerName")'; 
0

嘗試。

<script type="text/javascript"> 
function DoRedirect() { 
window.location.href = 'http://www.google.com'; 
} 
</script>