如何使用jQuery设置输入文本的值


349

我有一个输入文本是这样的:

<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>

哪些产生以下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>

我想使用jquery设置此输入文​​本的值,所以我做到了:

<script type="text/javascript" language="javascript">
    $(function() {
        $('.textBoxEmployeeNumber').val("fgg");
    });
</script> 

但是,它不起作用...我的语法有什么错误?


1
我使用的形式内的输入文本....
raberana

Answers:


528

您的选择器正在检索文本框的周围,<div class='textBoxEmployeeNumber'>而不是其内部的输入。

// Access the input inside the div with this selector:
$(function () {
  $('.textBoxEmployeeNumber input').val("fgg");
});

看到输出HTML后更新

如果ASP.NET代码可靠地输出<input>具有id属性的HTML id='EmployeeId',则可以更简单地使用:

$(function () {
  $('#EmployeeId').val("fgg");
});

如果失败,则需要在浏览器的错误控制台中确认没有其他脚本错误导致此操作失败。上面的第一个示例在此演示中正常工作。


尝试了,不起作用。...也尝试了.textBoxEmployeeNumber input = [type =“ text”] ...也不起作用
raberana 2012年

@using(Html.BeginForm()){@ Html.ValidationSummary(true)<fieldset> <legend> Reservation </ legend> ......... <div class =“ editor-label”> @Html。 LabelFor(model => model.EmployeeId,“雇员编号”)</ div> <div class =“ editor-field textBoxEmployeeNumber”> @ Html.EditorFor(model => model.EmployeeId)@ Html.ValidationMessageFor(model =>模型.EmployeeId)</ div> ..........
raberana 2012年

<div class =“ editor-label”> <label for =“ EmployeeId”>员工编号</ label> </ div> <div class =“ editor-field textBoxEmployeeNumber”> <input class =“ text-box单行“” data-val =“ true” data-val-number =“字段EmployeeId必须为数字。“ data-val-required =”员工ID字段为必填。“ id =“ EmployeeId” name =“ EmployeeId” type =“ text” value =“” /> <span class =“ field-validation-valid” data-valmsg-for =“ EmployeeId” data-valmsg-replace =“ true” > </ span> </ div>
raberana

@RojBeraña浏览器是否读取$(document).ready函数中的代码?放置警报:<script type =“ text / javascript” language =“ javascript”> alert('entered'); $(function(){$('#EmployeeId')。val(“ fgg”);}); </ script>
karaxuna 2012年

有类似的问题,值在浏览器中可见,但是当我用F12检查代码时,value=""保存后在DB中有空文件。
Sebastian XaweryWiśniowiecki2015年

67

使用jQuery,我们可以使用以下代码:

通过输入名称选择:

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

按输入类别选择:

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

通过输入ID选择:

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

12
$(document).ready(function () {
    $('#EmployeeId').val("fgg");

    //Or
    $('.textBoxEmployeeNumber > input').val("fgg");

    //Or
    $('.textBoxEmployeeNumber').find('input').val("fgg");
});


4

这是上课的

$('.nameofdiv').val('we are developers');

身份证

$('#nameofdiv').val('we are developers');

现在,如果您有格式的iput,则可以使用

$("#form li.name input.name_val").val('we are awsome developers');

0

这是文件上传的另一个变体,其引导按钮比默认文件上传浏览按钮好看。这是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>

这是脚本:

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

0

用纯js会更简单

EmployeeId.value = 'fgg';

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.