首页 > 解决方案 > 如何将选项的值解析为输入值

问题描述

很抱歉,这个问题有点像这个问题的重复:Parse value of option into html attribute但我不知道如何更改我的问题的答案。我正在尝试做同样的事情,只是我试图不将其解析为“数据销售产品”,而我试图将其放入“价值”中。

这是应将值插入到的按钮的代码:

<form class="flex-c-m stext-101 cl0 size-101 bg1 bor1 hov-btn1 p-lr-15 trans-04" name="PrePage" method = "post" action = "https://scotest.authorize.net/payment/CatalogPayment.aspx"> <input class="js-addcart-detail" type = "hidden" name = "LinkId" value ="" /> <input type = "image" src ="//testcontent.authorize.net/images/buy-now-gold.gif" /> </form> 

其余的与其他问题完全相同

我尝试更改此代码:

<script type="text/javascript">
function getComboA(selectObject) {
const button = document.querySelector('button.js-addcart-detail');
button.dataset.sellyProduct = selectObject.value;
console.log(button);
}
</script>

对此:

<script type="text/javascript">
function getComboA(selectObject) {
const button = document.querySelector('form.js-addcart-detail');
form.value = selectObject.value;
console.log(button);
}
</script>

开发者控制台中的错误消息:

ReferenceError: form is not defined

但这不起作用。

标签: javascriptphphtmlajax

解决方案


If I don't misunderstood your question, then this is what you want to achieve. You basically want to set the selected option's value on a hidden input fields value.

function getComboA(selectObject) {
  const input = document.querySelector('input.js-addcart-detail');
  input.value = selectObject.value;
  console.log(input);
}
<select class="js-select2" name="time" id="comboA" onchange="getComboA(this)">
  <option value="">Choose an option</option>
  <option value="3cffe13b">Size S</option>
  <option value="M">Size M</option>
  <option value="L">Size L</option>
  <option value="XL">Size XL</option>
</select>

<input class="js-addcart-detail" type="hidden" name="LinkId" value="" />
<button class="flex-c-m stext-101 cl0 size-101 bg1 bor1 hov-btn1 p-lr-15 trans-04 js-addcart-detail">Add to cart</button>


推荐阅读