[英]How can i get multiple value using dropdown select option data attribute?
I want to get two data using data attribute and display the two value in two separate input field readonly.我想使用数据属性获取两个数据,并在两个单独的输入字段中以只读方式显示这两个值。
I can get one value properly.我可以正确地获得一个值。 data-price, but I can't get data-saleP.数据价格,但我无法获得数据销售。
$('.selectProduct').on('change', function() { $('.regularPrice').val( $(this).find(':selected').data('price') ); });
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <select id="selectProduct" class="form-control selectProduct" name="productId"> <option selected="">Select Product</option> <option data-price="150" data-salep="50" value="1">Product 1</option> <option data-price="120" data-salep="110" value="2">Product 2</option> </select> <div class="row"> <div class="col-md-6"> <div class="form-group form-group-sm"> <label>Regular Price</label> <input type="text" readonly="" class="form-control regularPrice" id="regularPrice" name="regularPrice" value=""> </div> </div> <div class="col-md-6"> <div class="form-group form-group-sm"> <label>Sale Price</label> <input type="text" readonly="" class="form-control salePrice" id="salePrice" name="salePrice" value=""> </div> </div> </div>
It's simple, use same code with salep
like:很简单,使用与salep
相同的代码,例如:
$('.selectProduct').on('change', function() { $('.regularPrice').val( $(this).find(':selected').data('price') ); $('.salePrice').val( $(this).find(':selected').data('salep') ); });
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <select id="selectProduct" class="form-control selectProduct" name="productId"> <option selected="">Select Product</option> <option data-price="150" data-salep="50" value="1">Product 1</option> <option data-price="120" data-salep="110" value="2">Product 2</option> </select> <div class="row"> <div class="col-md-6"> <div class="form-group form-group-sm"> <label>Regular Price</label> <input type="text" readonly="" class="form-control regularPrice" id="regularPrice" name="regularPrice" value=""> </div> </div> <div class="col-md-6"> <div class="form-group form-group-sm"> <label>Sale Price</label> <input type="text" readonly="" class="form-control salePrice" id="salePrice" name="salePrice" value=""> </div> </div> </div>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.