如何改变输入值

时间:2016-01-14 10:01:26

标签: javascript html

我有以下html结构:

<div class="checkout-related-view__related-row last">
  <div class="checkout-related-view__related-row-cell checkout-related-view__related-row-cell--left">
    <input class="checkbox related-checkbox" data-role="none" id="related-checkbox-7085508-020-01077" type="checkbox" value="10">
    <label for="related-checkbox-7085508-020-01077"><span class="checkbox"></span> Old Bed or Mattress Recycling</label>
  </div>
  <div class="checkout-related-view__related-row-cell checkout-related-view__related-row-cell--right">
    <span class="price"><span class="currency">£</span>39</span> <a class="checkout-icon checkout-icon-info related-info" data-content="We will collect your old bed frame, mattress, divan base and headboard, and recycle as much as possible into new material."
    data-html="true" data-original-title="Recycling Service" data-toggle="popover" data-trigger="focus" href="javascript:void(0)" tabindex="0" title=""><span class="visuallyhidden">Recycling Service</span></a>
  </div>
  <div class="checkout-related-view__related-row-cell checkout-related-view__related-row-cell--qty">
    <div class="input-combobox input-combobox__with-qty" data-label="Qty" data-range-max="1" data-range-min="0">
      <span class="input-combobox__label">Qty</span>
      <input class="input-combobox__text input-qty" name="related_products[7085508][10][qty]" type="text" value="0">
    </div>
  </div>
</div>

我尝试使用JS来更改QTY。不幸的是,没有运气。 请注意,该代码没有ID。有什么方法可以使用名称或类来设置值吗?

我尝试过但没有用的代码是:

document.getElementsByClassName("input-combobox__text input-qty").value="1";

你能帮忙吗?

2 个答案:

答案 0 :(得分:1)

Number返回一个元素数组,如果你想设置第一个匹配的add那么

getClass

答案 1 :(得分:1)

getElementsByClassName会返回一个元素数组,您无法将value应用于数组。相反,抓住返回数组中的第一项:

document.getElementsByClassName("input-combobox__text input-qty")[0].value = "1";