Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
205 views
in Technique[技术] by (71.8m points)

javascript - 如何使用jQuery设置输入文本的值(How to set value of input text using 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> Which produce following html(哪些产生以下HTML) <div class="editor-label"> <label for="EmployeeId">Employee Number</label> </div> <div class="editor-field textBoxEmployeeNumber"> <input class="text-box single-line" data-val="true" data-val-number="The field EmployeeId must be a number." data-val-required="The EmployeeId field is required." id="EmployeeId" name="EmployeeId" type="text" value="" /> <span class="field-validation-valid" data-valmsg-for="EmployeeId" data-valmsg-replace="true"></span> </div> I want to set the value of this input text using jquery so i did this:(我想使用jquery设置此输入文??本的值,所以我做到了:) <script type="text/javascript" language="javascript"> $(function() { $('.textBoxEmployeeNumber').val("fgg"); }); </script> however, it is not working... what is the error in my syntax?(但是,它不起作用...我的语法有什么错误?)   ask by raberana translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Your selector is retrieving the text box's surrounding <div class='textBoxEmployeeNumber'> instead of the input inside it.(您的选择器正在检索文本框周围的<div class='textBoxEmployeeNumber'>而不是其中的输入。)

// Access the input inside the div with this selector: $(function () { $('.textBoxEmployeeNumber input').val("fgg"); }); Update after seeing output HTML(看到输出HTML后更新) If the ASP.NET code reliably outputs the HTML <input> with an id attribute id='EmployeeId' , you can more simply just use:(如果ASP.NET代码可靠地输出具有id属性id='EmployeeId'的HTML <input> ,则可以更简单地使用:) $(function () { $('#EmployeeId').val("fgg"); }); Failing this, you will need to verify in your browser's error console that you don't have other script errors causing this to fail.(如果失败,则需要在浏览器的错误控制台中确认没有其他脚本错误导致此操作失败。) The first example above works correctly in this demonstration.(上面的第一个示例在此演示中正常工作。)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...