Append select option with an HTML5 data attribute using jQuery

為{幸葍}努か 提交于 2019-12-01 05:01:38

问题


I'm trying to construct a set of options for a select element, using jQuery. It's working except for the data attribute that should be included, which seems to being ommitted.

Here is my code:

// cJ is JSON object.
// #currency is my select element.
function makeCurDropDown(cJ) {
    $.each(cJ, function (i) {
        $('#currency').append(
            $('<option></option>').val(i).data("sym", cJ[i][0]).html(cJ[i][1])
        );
    });
}

回答1:


Actually the data is set, but since you are using data method instead of the attr method, it's not visible. jQuery stores the value behind the scenes, for getting the value you can use .data('sym'). If you want to set the value as an attribute you can use attr method instead.



来源:https://stackoverflow.com/questions/18254213/append-select-option-with-an-html5-data-attribute-using-jquery

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!