简体   繁体   English

如何使用下拉列表 select 选项数据属性获取多个值?

[英]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.

相关问题 使用Dalekjs测试工具,当Option标记中没有“值”属性时,如何在Dropdown(选择元素)中选择Option? - Using Dalekjs test tool, how to select Option in Dropdown(Select element) when there is no “value” attribute in Option tag? 如何从多个选择的选项中获取数据属性? - How to get data attribute from option in multiple select? 更改选项时如何从表中动态创建的选择中获取 data-* 属性的值 - How to get the value of an data-* attribute from a dynamically created select in a table when I change an option 如何使用jQuery从AJAX生成的选择下拉列表中获取文本选项? - How can i get text option from AJAX generated select dropdown using jQuery? 如何使用 jQuery 过滤下拉 select 选项? - How can I filter dropdown select option with using jQuery? 使用数据属性和jQuery,如何通过数据属性名称和数据属性值选择元素 - Using data attributes and jQuery how can I select elements by data attribute name AND data attribute value 如何使用 vue.js 在 select 选项中添加数据图标属性 - How I can add data-icon attribute in select option using vue.js 如何获取多选项下拉菜单的值? - How to get the value of a multiple option dropdown? 如何选择 <option>在按值下拉列表中,并为其分配“选定”属性? - How to select an <option> within a dropdown by value and assign the “selected” attribute to it? 如何使用getElementsByClassName确定所选下拉选项的值? - How can I determine the value of the selected dropdown option using getElementsByClassName?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM