使用html选择值更新javascript

时间:2016-06-19 05:15:11

标签: javascript jquery html

这是我的代码:

<select name="points">
  <option value="5">5 points</option>
  <option value="10">10 points</option>
  <option value="50">50 points</option>
</select>

这是我的Javascript代码:

<script
  src="https://checkout.stripe.com/checkout.js" class="stripe-button"
  data-key="test key"
  data-amount="option value here" // i need help here
  data-name="Site Name"
  data-description="2 widgets ($20.00)"
  data-image="/128x128.png">
</script>

javascript代码是弹出窗口的按钮。我想获取select选项值,我想将其插入到js代码中的数据量中。我怎么能这样做?

2 个答案:

答案 0 :(得分:1)

使用change方法检测选择列表的值,然后将其应用于按钮的data-amount属性。

$(document).ready(function(){
    $('[name=points]').change(function(){
      var newAmount=$(this).val();
      $('.stripe-button').data('amount',newAmount);
    })
});

答案 1 :(得分:0)

<script src="https://code.jquery.com/jquery-1.12.4.min.js"></script>
<script>
    function updata(){
var point_val = $("#points option:selected").val();
alert(point_val);
document.querySelector("script").setAttribute("data-amount",point_val);
}

</script>
<select name="points" id="points" onchange="updata();">
  <option value="5">5 points</option>
  <option value="10">10 points</option>
  <option value="50">50 points</option>
</select>
<script
  src="https://checkout.stripe.com/checkout.js" class="stripe-button"
  data-key="test key"
  data-amount="option value here"
  data-name="Site Name"
  data-description="2 widgets ($20.00)"
  data-image="/128x128.png">
</script>

<script src="https://code.jquery.com/jquery-1.12.4.min.js"></script>
<script>
	function updata(){
var point_val = $("#points option:selected").val();
alert(point_val);
document.querySelector("script").setAttribute("data-amount",point_val);
}
	
</script>
<select name="points" id="points" onchange="updata();">
  <option value="5">5 points</option>
  <option value="10">10 points</option>
  <option value="50">50 points</option>
</select>
<script
  src="https://checkout.stripe.com/checkout.js" class="stripe-button"
  data-key="test key"
  data-amount="option value here"
  data-name="Site Name"
  data-description="2 widgets ($20.00)"
  data-image="/128x128.png">
</script>