繁体   English   中英

jQuery函数从选择框中选择自定义属性

[英]jquery function select custom attribute from select box

我正在尝试从选择框中获取自定义属性值。 它由我已经在工作的复选框单击触发。 我可以很好地获得名称和值对。 我想从选项值行中获取自定义属性(治疗)(强度)(重量)和(障碍)。 这可能吗?

选择框

<option value="2220" therapy="1" strength="1" weight="0" obstacle="0">Supine Calf/Hamstring Stretch</option>
<option value="1415" therapy="0" strength="0" weight="0" obstacle="0">Sitting Chair Twist</option>
<option value="1412" therapy="0" strength="0" weight="0" obstacle="0">Static Abductor Presses</option>

jQuery的

// exercise list filter category         
jQuery.fn.filterByCategory = function(checkbox) {
        return this.each(function() {
            var select = this;
            var optioner = [];

            $(checkbox).bind('click', function() {

                var optioner = $(select).empty().scrollTop(0).data('options');

                var index=0;
                $.each(optioner, function(i) {

                    var option = optioner[i];
                    var option_text = option.text;
                    var option_value = parseInt(option.value);

                        $(select).append(
                           $('<option>').text(option.text).val(option.value)
                        );

                    index++;
                }); 
            });
        });
    };

您需要找到选中的,如下所示:

var $select = $('#mySelectBox');
var option = $('option:selected', $select).attr('mytag');

那就是获得选择的选项属性的方法:

$('select').find(':selected').attr('weight')

获取选择的选项并使用attr函数获取属性:

$("select").find(":selected").attr("therapy")

JSFIDDLE

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM