简体   繁体   中英

How to change value of input

I have the following html structure:

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

I've try using JS to change the QTY. Unfortunatelly there is no luck. Please note that the code has no ID. Is there any way how I can set the value using name or class?

The code I've tried and didn't work is :

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

Can you please help?

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

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

getElementsByClassName returns an array of elements, and you can't apply value to an array. Instead, grab the first item in the returned array:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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