[英]How to change dynamically input value attribute based on select option selected?
I need to update dynamically the input value attribute with data-price value based on option selection using jQuery. 我需要基于基于jQuery的选项选择,使用数据价格值动态更新输入值属性。
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <script type="text/javascript"> $(".select").change(function () { newPrice = $(this).children(':selected').data('price'); console.log(newPrice); $(this).next('input').focus().val(newPrice); }); </script> <select class="select"> <option value="1" data-price="2.99">1</option> <option value="2" data-price="3.99">2</option> <option value="3" data-price="4.99">3</option> <option value="4" data-price="5.99">4</option> </select> <input type="text" value="">
Thanks in Advance. 提前致谢。
You can use jQuery's .attr()
to get/set the attribute . 您可以使用jQuery的
.attr()
来获取/设置属性 。
Please Note: You also have to place the code after the HTML or warp the code with $(document).ready(function(){....})
请注意:您还必须将代码放在HTML后面,或使用
$(document).ready(function(){....})
将代码扭曲
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <script> $(document).ready(function(){ $(".select").change(function () { newPrice = $(this).children(':selected').data('price'); $(this).next('input').focus().attr('value', newPrice); console.log('Element:', $(this).next('input')[0]); }); }); </script> <select class="select"> <option value="1" data-price="2.99">1</option> <option value="2" data-price="3.99">2</option> <option value="3" data-price="4.99">3</option> <option value="4" data-price="5.99">4</option> </select> <input type="text" value="">
put your script at the bottom so it will apply the property change() 将您的脚本放在底部,以便它将应用属性change()
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select class="select">
<option value="1" data-price="2.99">1</option>
<option value="2" data-price="3.99">2</option>
<option value="3" data-price="4.99">3</option>
<option value="4" data-price="5.99">4</option>
</select>
<input type="text" value="">
<script type="text/javascript">
$(".select").change(function () {
newPrice = $(this).children(':selected').data('price');
console.log(newPrice);
$(this).next('input').focus().val(newPrice);
});
</script>
When your code is executed, there is no .select
element (the DOM is not ready). 执行代码时,没有
.select
元素(DOM未准备好)。
Wrap you code inside 将您的代码包装在里面
$(function(){...})
or use 或使用
$(document).on('change', '.select', function(){.....})
to set the listener. 设置监听器。
Here is an example: 这是一个例子:
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <script type="text/javascript"> $(function() { $(".select").change(function() { newPrice = $(this).children(':selected').data('price'); console.log(newPrice); $(this).next('input').focus().val(newPrice); }); }); </script> <select class="select"> <option value="1" data-price="2.99">1</option> <option value="2" data-price="2.99">2</option> <option value="3" data-price="2.99">3</option> <option value="4" data-price="2.99">4</option> </select> <input type="text" value="">
<select id="selectItem">
<option value="1" data-price="2.99">1</option>
<option value="2" data-price="2.99">2</option>
<option value="3" data-price="2.99">3</option>
<option value="4" data-price="2.99">4</option>
</select>
<input type="text" value="">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
$("#selectItem").on('change',function(){
var newPrice=$(this).find('option').data('price');
console.log(newPrice)
$("input").val(newPrice)
})
})
</script>
Since you don't have IDs So you can use like this with you code. 由于您没有ID,因此您可以将其与代码一起使用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.