向输入中添加文本

时间:2018-08-31 16:11:27

标签: jquery html

我不确定为什么不能将文本放置在输入字段中。

$('#btn').on('click', () => {
  let result = $('.form-group input[type="text"]');
  for (i = 0; i < result.length; i++) {
    result[i].innerText = "Test";
    //result[i].text = "Test";
    //result[i].Value = "Test";
    //result[i].val("Test");
    //result[i].text("Test");
    console.log(result[i]);
  }
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="form-group">
  <label for="txtBusinessName" class="control-label col-lg-3 col-sm-4">Business</label>
  <div class="col-lg-9 col-sm-8">
    <input id="txtBusinessName" class="form-control input-sm" name="BusinessName" type='text' placeholder="Business Name" style="max-width:75%;" />
  </div>
</div>

<div class="form-group">
  <label for="txtTenantID" class="control-label col-lg-3 col-sm-4">TenantID</label>
  <div class="col-lg-9 col-sm-8">
    <input id="txtTenantID" class="form-control input-sm" name="TenantID" placeholder="TenantID" style="max-width:75%;" type='text' />
  </div>
</div>

<div class="form-group">
  <label for="txtPrimaryPointOfContactEmail" class="control-label col-lg-3 col-sm-4" style="padding:7px 18px 0px 0px;">Contact Email</label>
  <div class="col-lg-9 col-sm-8">
    <input id="txtPrimaryPointOfContactEmail" name="PrimaryPointOfContactEmail" type='text' class="form-control input-sm" placeholder="Primary Point Of Contact Email" style="max-width:75%;" />
  </div>
</div>


<button id='btn' class='btn btn-success'>
Click Me
</button>

1 个答案:

答案 0 :(得分:2)

.val()可以使用,但是您需要像results一样将$(result[i]).val("Test");包装在jQuery中。普通JS也可以,但是您不能大写value(即result[i].value = "Test";也可以)。

$('#btn').on('click', () => {
  let result = $('.form-group input[type="text"]');
  for (i = 0; i < result.length; i++) {
    $(result[i]).val("Test");
  }
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="form-group">
  <label for="txtBusinessName" class="control-label col-lg-3 col-sm-4">Business</label>
  <div class="col-lg-9 col-sm-8">
    <input id="txtBusinessName" class="form-control input-sm" name="BusinessName" type='text' placeholder="Business Name" style="max-width:75%;" />
  </div>
</div>

<div class="form-group">
  <label for="txtTenantID" class="control-label col-lg-3 col-sm-4">TenantID</label>
  <div class="col-lg-9 col-sm-8">
    <input id="txtTenantID" class="form-control input-sm" name="TenantID" placeholder="TenantID" style="max-width:75%;" type='text' />
  </div>
</div>

<div class="form-group">
  <label for="txtPrimaryPointOfContactEmail" class="control-label col-lg-3 col-sm-4" style="padding:7px 18px 0px 0px;">Contact Email</label>
  <div class="col-lg-9 col-sm-8">
    <input id="txtPrimaryPointOfContactEmail" name="PrimaryPointOfContactEmail" type='text' class="form-control input-sm" placeholder="Primary Point Of Contact Email" style="max-width:75%;" />
  </div>
</div>


<button id='btn' class='btn btn-success'>
Click Me
</button>