如何使用jQuery设置输入文本的价值 [英] How to set value of input text using jQuery
本文介绍了如何使用jQuery设置输入文本的价值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个输入的文本是这样的:
I have an input text which is this:
<div class="editor-label">
@Html.LabelFor(model => model.EmployeeId, "Employee Number")
</div>
<div class="editor-field textBoxEmployeeNumber">
@Html.EditorFor(model => model.EmployeeId)
@Html.ValidationMessageFor(model => model.EmployeeId)
</div>
我想使用jQuery,所以我这样做是为了设置此输入文本的值:
I want to set the value of this input text using jquery so i did this:
<script type="text/javascript" language="javascript">
$(function() {
$('.textBoxEmployeeNumber').val("fgg");
});
</script>
但是,它不工作...什么是我的语法错误?
however, it is not working... what is the error in my syntax?
推荐答案
您选择被检索的文本框的周围的&LT; DIV CLASS ='textBoxEmployeeNumber'&GT;
而不是里面的输入。
Your selector is retrieving the text box's surrounding <div class='textBoxEmployeeNumber'>
instead of the input inside it.
// Access the input inside the div with this selector:
$(function () {
$('.textBoxEmployeeNumber input').val("fgg");
});
看到输出HTML 在
更新
如果在ASP.NET code可靠输出HTML &LT;输入&GT;
与id属性 ID ='雇员'
,你可以更简单地只使用:
Update after seeing output HTML
If the ASP.NET code reliably outputs the HTML <input>
with an id attribute id='EmployeeId'
, you can more simply just use:
$(function () {
$('#EmployeeId').val("fgg");
});
如果做不到这一点,你需要在浏览器的错误控制台,以验证您不必造成这一失败的其他脚本错误。上述在这个示范工作正常。
这篇关于如何使用jQuery设置输入文本的价值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文