簡體   English   中英

C# - 如何將 html 輸入值傳遞給 url.Action

[英]C# - How to pass html input value to url.Action

我有一個input和一個button 輸入的值應該傳遞給@url.Action就像下面代碼中的描述:

<input class="in-class" id="textbox" type="text" runat="server" />
<button class="btn-class" id="press" type="button" onclick="location.href='@Url.Action("Index", "Home", new {id = /*Value Of textbox*/ })'" >click here</button>

正如我在代碼中提到的, /*Value Of textbox*/應該是輸入的當前值。

使用 jQuery 或 JavaScript 更改 href 值,如下所示:

     <button class="btn-class" id="press" type="button" onclick="changeHref()" >click here</button>

   function changeHref(){
         var url = '@Url.Action("Index", "Home")';
         var txtVal = $('#textbox').val();
         window.location.href = url + '/?txtVal=' + txtVal;
   }

我用這個表格

<input type="text" id="txtValue" /> <input type="button" value="Detail" onclick="location.href='@Url.Action("Action", "Home")?Value=' + $('#txtValue').val()" />

或者你不能在 jquery 函數中寫這個。

我更喜歡使用 jQuery click處理程序,因為您有按鈕 ID 並遵循標准事件注冊模型來分隔 HTML 和 JS

HTML

<input class="in-class" id="textbox" type="text" />
<button class="btn-class" id="press" type="button">click here</button>

JS

$('#press').click(function () {
   var url = '@Url.Action("Index", "Home")';
   var textValue = $('#textbox').val();

   window.location.href = url + '?id=' + textValue;
});

PS:無需在 MVC 中使用runat="server"屬性,因為 Razor 不需要它。

暫無
暫無

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

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