javascript 如何使用javascript从文本框中获取文本
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17336268/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get text from a textbox using javascript
提问by sanzy
i have developed a web application using mvc4.
我已经使用 mvc4 开发了一个 Web 应用程序。
in this case i need to get a text box value(actually the text entered in the text box) using javascript.
在这种情况下,我需要使用 javascript 获取文本框值(实际上是在文本框中输入的文本)。
here is the code i am using
这是我正在使用的代码
@model PortalModels.WholeSaleModelUser
@using (Html.BeginForm("uploadFile", "WholeSaleTrade", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
@Html.ValidationSummary(true)
<fieldset>
<legend>WholeSaleModelUser</legend>
<table>
<tr>
<td>
<div class="editor-label">
@Html.LabelFor(model => model.Name)
</div>
</td>
<td>
<div class="editor-field">
@Html.TextBoxFor(model => model.Name)
@Html.ValidationMessageFor(model => model.Name)
</div>
</td>
</tr>
</table>
<div id="partial">
<table>
<tr>
<td>
<img id="blah" src="../../Images/no_image.jpg" alt="your image" height="200px" width="170px" />
</td>
</tr>
</table>
<script type="text/javascript">
function loadUserImage() {
var userImg = document.getElementById("blah");
var imgNm = $("#Name").value;
userImg.src = "D:/FEISPortal/FortalApplication/Img/" + imgNm + ".png";
alert(imgNm);
alert(userImg.src);
}
</script>
in that case alert gives the value as "undefined" and if do the following modification alert gives nothing.
在这种情况下,警报给出的值为“未定义”,如果执行以下修改警报,则什么也没有。
var imgNm = document.getElementById("Name").value;
for
为了
var imgNm = $("#Name").value;
how to get the text entered in the text box?
如何获取文本框中输入的文本?
回答by Ufuk Hac?o?ullar?
回答by Anton
If you're using native javascript you should use this:
如果你使用原生 javascript,你应该使用这个:
var userImgNameElement = document.getElementById("userImgNameId");
var userImgNameValue = userImgNameElement.getAttribute("value");
With "onChanged" event:
使用“onChanged”事件:
addEvent(document.getElementById('userImgNameId'), 'change', function(event) {
var userImgNameValue = event.target.getAttribute("value");
});
回答by Sumurai8
In an input
element, the value of can be found in a value
property of that element. It can therefore be retrieved via document.getElementById('idOfElement').value
.
在input
元素中,可以在value
该元素的属性中找到 的值。因此可以通过 检索它document.getElementById('idOfElement').value
。
In an textarea
element has it's value between the starting and the closing tag. Therefore, the value
property is empty. In raw javascript, you would be able to retrieve the contents with document.getElementById('idOfElement').innerText
.
在一个textarea
元素中,它的值在开始和结束标记之间。因此,该value
属性为空。在原始 javascript 中,您可以使用document.getElementById('idOfElement').innerText
.
If you are however using jQuery, Ufuk's solution with .val()
is much easier to use.
但是,如果您使用的是 jQuery,则 Ufuk 的解决方案.val()
更易于使用。