使用javascript从html文本框中获取值

时间:2022-04-04 14:01:41

I have created an ASP.NET MVC app and would like to do some "stuff" with a value in the textbox before its sent back to the controller.

我创建了一个ASP。NET MVC应用程序,并想做一些“东西”在文本框中的值,然后再发送回控制器。

So I have a textbox in a form:

我有一个文本框:

using (Html.BeginForm("HandleForm","Home")
{
    <p>Enter Value<p>
    @Html.TextBox("amount")
    <input type="submit" value="Submit" />
}

normally i would use something like this in JS:

通常我会在JS中使用这样的东西:

var value = document.getElementById(ID).value;

So how would I do this with the HTML Helpers? is it possible to give this textbox an id?

那么我该如何使用HTML助手呢?可以给这个文本框一个id吗?

3 个解决方案

#1


1  

You can't get an HTML Helper for that, but this might help you.

你不能得到一个HTML助手,但这可能会帮助你。

var value = document.getElementById('@Html.Id("amount")').value;

using the @Html.Id() Html Helper you will get the id of that specific viewData

使用@Html.Id() Html助手,您将获得该特定视图数据的id。

#2


0  

we can specify additional html attributes when using html helpers in mvc

在mvc中使用html helper时,我们可以指定其他html属性

http://msdn.microsoft.com/en-us/library/dd493049(v=vs.118).aspx

http://msdn.microsoft.com/en-us/library/dd493049(v = vs.118). aspx

The html attributes are specified as simple object eg

html属性被指定为简单对象eg

new { id = "text1", maxlength="20", size="15" }

Method Signature

方法签名

public static MvcHtmlString TextBox(
    this HtmlHelper htmlHelper,
    string name,
    Object value,
    Object htmlAttributes
)

#3


0  

Ofcourse you can assign Id, Class and various Html attributes to any Html Helper as these helpers are parsed to normal html tags.

当然,您可以将Id、类和各种Html属性分配给任何Html助手,因为这些助手被解析为普通的Html标记。

E.g

@Html.TextBoxFor(m=>m.amount,new { id = "txtAmount", class="anyClass" })

or

@Html.TextBox(m=>m.amount,null,new { id = "txtAmount", class="anyClass" })

This is parsed to

这是解析

<input id="txtAmount" name="amount" class="anyClass" type="text" />

Then in javascript you can access value of this control as

然后在javascript中,您可以访问这个控件的值as

  var value = document.getElementById("txtAmount").value;

#1


1  

You can't get an HTML Helper for that, but this might help you.

你不能得到一个HTML助手,但这可能会帮助你。

var value = document.getElementById('@Html.Id("amount")').value;

using the @Html.Id() Html Helper you will get the id of that specific viewData

使用@Html.Id() Html助手,您将获得该特定视图数据的id。

#2


0  

we can specify additional html attributes when using html helpers in mvc

在mvc中使用html helper时,我们可以指定其他html属性

http://msdn.microsoft.com/en-us/library/dd493049(v=vs.118).aspx

http://msdn.microsoft.com/en-us/library/dd493049(v = vs.118). aspx

The html attributes are specified as simple object eg

html属性被指定为简单对象eg

new { id = "text1", maxlength="20", size="15" }

Method Signature

方法签名

public static MvcHtmlString TextBox(
    this HtmlHelper htmlHelper,
    string name,
    Object value,
    Object htmlAttributes
)

#3


0  

Ofcourse you can assign Id, Class and various Html attributes to any Html Helper as these helpers are parsed to normal html tags.

当然,您可以将Id、类和各种Html属性分配给任何Html助手,因为这些助手被解析为普通的Html标记。

E.g

@Html.TextBoxFor(m=>m.amount,new { id = "txtAmount", class="anyClass" })

or

@Html.TextBox(m=>m.amount,null,new { id = "txtAmount", class="anyClass" })

This is parsed to

这是解析

<input id="txtAmount" name="amount" class="anyClass" type="text" />

Then in javascript you can access value of this control as

然后在javascript中,您可以访问这个控件的值as

  var value = document.getElementById("txtAmount").value;