繁体   English   中英

如何更改输入值

[英]How to change value of input

我有以下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更改数量。 不幸的是,没有运气。 请注意,该代码没有ID。 有什么方法可以使用名称或类设置值?

我尝试过但不起作用的代码是:

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

你能帮忙吗?

如果要设置第一个匹配项的valuedocument.getElementsByClassName返回一个元素数组。

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

getElementsByClassName返回一个元素数组,并且您不能将value应用于数组。 相反,获取返回数组中的第一项:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM