本文翻译自: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? 但是,它不起作用...我的语法有什么错误?


#1楼

参考:https://stackoom.com/question/iWRu/如何使用jQuery设置输入文本的值


#2楼

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. 上面的第一个示例在此演示中正常工作。


#3楼

$(document).ready(function () {$('#EmployeeId').val("fgg");//Or$('.textBoxEmployeeNumber > input').val("fgg");//Or$('.textBoxEmployeeNumber').find('input').val("fgg");
});

#4楼

Using jQuery, we can use the following code: 使用jQuery,我们可以使用以下代码:

Select by input name: 通过输入名称选择:

$('input[name="textboxname"]').val('some value')

Select by input class: 按输入类别选择:

$('input[type=text].textboxclass').val('some value')

Select by input id: 通过输入ID选择:

$('#textboxid').val('some value')

#5楼

For element with id 对于ID为的元素

<input id="id_input_text16" type="text" placeholder="Ender Data"></input>

You can set the value as 您可以将值设置为

$("#id_input_text16").val("testValue");

Documentation here . 文档在这里 。


#6楼

Here is another variation for a file upload that has a nicer looking bootstrap button than the default file upload browse button. 这是文件上传的另一个变体,它的引导按钮比默认的文件上传浏览按钮好看。 This is the html: 这是html:

<div class="form-group">@Html.LabelFor(model => model.FileName, htmlAttributes: new { @class = "col-md-2  control-label" })<div class="col-md-1 btn btn-sn btn-primary" id="browseButton" onclick="$(this).parent().find('input[type=file]').click();">browse</div><div class="col-md-7"><input id="fileSpace" name="uploaded_file"  type="file" style="display: none;">   @*style="display: none;"*@@Html.EditorFor(model => model.FileName, new { htmlAttributes = new { @class = "form-control", @id = "modelField"} })@Html.ValidationMessageFor(model => model.FileName, "", new { @class = "text-danger" })</div></div>

Here is the script: 这是脚本:

        $('#fileSpace').on("change", function () {$("#modelField").val($('input[name="uploaded_file"]').val());

如何使用jQuery设置输入文本的值相关推荐

  1. JQUERY设置或返回属性值attr

    1.attr:设置或返回属性值 $(selector).attr(attribute) $(selector).attr(attribute,value) $(selector).attr(attri ...

  2. jquery 设置select的默认值

    <select id="sel" ><option value="s1" > aaaa </option><optio ...

  3. jQuery监听文本框值改变触发事件(propertychange)

    完整代码: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w ...

  4. Jquery设置属性值

    //最经常用到的jquery设置 标签的属性值 常用到的方法 function setProperty(){ $('#DIV3').attr("readonly",true);// ...

  5. Flutter TextField 边框样式以及提示文本 、Flutter输入文本TextField

    题记 -- 执剑天涯,从你的点滴积累开始,所及之处,必精益求精. github? 你可能需要 百度同步 CSDN 网易云课堂教程 掘金 知乎 Flutter系列文章 头条同步 本文章首发于微信公众号( ...

  6. jQuery设置和获取HTML、文本和值

    <script type="text/javascript">//<![CDATA[ $(function(){//获取<p>元素的HTML代码$(& ...

  7. 在输入文本框中获取值

    使用jQuery获取和呈现输入值的方法有哪些? 这是一个: <script type="text/javascript" src="http://code.jque ...

  8. JQueryDOM之设置和获取HTML、文本和值

    设置和获取HTML.文本和值 html()方法:获得或设置某个元素的html元素      $(selector).html() $("p").html();      该示例获得 ...

  9. jQuery—常用API(jQuery 属性操作、jQuery 文本属性值、jQuery 元素操作)

    1. jQuery 属性操作 1.1 设置或获取元素固有属性值 prop() 所谓元素固有属性就是元素本身自带的属性,比如 <a> 元素里面的 href,比如 <input> ...

最新文章

  1. 【跃迁之路】【674天】程序员高效学习方法论探索系列(实验阶段431-2018.12.19)...
  2. 设置(TableViewController)通用框架
  3. HighChart模拟点击series的name显示隐藏
  4. /etc/fstab 参数详解及如何设置开机自动挂载
  5. 笨方法学python3怎么样_有个很笨的女朋友,是怎么样的体验?
  6. List集合多线程并发条件下不安全,如何解决?
  7. ansible安装和基本使用
  8. 在线CSV转TSV工具
  9. 【原】IOS合并lib(.a)库的终极可用方法(可用于解决duplicate symbol静态库冲突)
  10. 2017 上半年总结
  11. 【闲聊产品】之一:半路出家的产品经理
  12. C程序设计案例(矩形法求定积分问题)
  13. 用陆地卫星 TM6数据演算 地表温度的单窗算法
  14. c4isr系统有无服务器,什么是C4ISR系统?
  15. 【供应链架构day11】京东履约系统的演进之路
  16. python k线斜率计算公式_浅析K线上涨斜率
  17. 走进C++程序世界-------类的定义和使用(数据成员和方法成员,析构函数,构造函数,内联实现)
  18. 安卓开发 投屏安卓设备到电脑端 scrcpy
  19. Nginx证书配置:cer文件和jks文件转nginx证书.crt和key文件
  20. 《中台产品经理宝典》读后感11:大白话说透时下备受追捧的数字化转型是什么?

热门文章

  1. Firebug无法添加到最新版firefox55.0.*中解决办法
  2. 两道关于回溯法,分支限界法的算法题
  3. [Be a Coding Plasterer] Components 1:get Basic Things
  4. C语言字符串操作总结大全(超详细)
  5. 网络技术沙龙:主题:数据库优化、CDN、集群负载均衡(1.9日技术聚会召集)
  6. PTA 数据结构与算法题目集 6-1
  7. 利用dex2jar反编译apk
  8. iOS开发拓展篇-XMPP简单介绍
  9. iOS $99 刀 开发者证书的申请步骤
  10. GNOME 2.18.2