【问题标题】:jQuery Swap data with Select Drop DownjQuery 使用 Select Drop Down 交换数据
【发布时间】:2017-03-09 06:44:42
【问题描述】:

对 jQuery 来说还很陌生,我一生都无法弄清楚为什么这不起作用..

<script> 
    jQuery('#my-quantity').change(function() {
    jQuery('#my-button').data('item-quantity', jQuery(this).val());}
</script>

<button class="snipcart-add-item" id="my-button" data-item-id="1" data-item-url="/"
    data-item-name="Item" data-item-price="20.00" data-item-quantity="">
   Buy now
</button>
<select id="my-quantity">
    <option>1</option>
    <option>2</option>
    <option>3</option>
</select>

【问题讨论】:

  • 您能否指出预期的行为是什么,以及它以何种方式不起作用。请阅读how to ask 获取指导。

标签: jquery select input


【解决方案1】:

将 jQuery 放在 html 下方并将其添加到结束 body 标记之前,即 &lt;/body&gt;

 $(document).on('change','#my-quantity', function() {
   $('#my-button').data('item-quantity', $(this).val());
 });

jQuery 替换为$,您也可以使用jQuery

【讨论】:

  • 我认为这可能是因为生成的文本必须是数字......我知道它是......但它是文本而不是数字?
  • @KylePaige 控制台中的任何错误。它也可以选择文本,因此错误在其他地方。如果你能创造一个小提琴,可能会?
猜你喜欢
  • 1970-01-01
  • 2017-01-01
  • 1970-01-01
  • 2019-07-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-03-05
  • 1970-01-01
相关资源
最近更新 更多