使用jQuery将选择选项附加到HTML5数据属性 [英] Append select option with an HTML5 data attribute using jQuery

查看:124
本文介绍了使用jQuery将选择选项附加到HTML5数据属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用jQuery为select元素构造一组选项。它的工作原理除了应该包含的数据属性外,似乎已被省略。

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.

这是我的代码:

// 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])
        );
    });
}


推荐答案

其实数据已设置,但由于您使用 data 方法而不是 attr 方法,它是不可见的。 jQuery存储幕后的值,用于获取可以使用的值 .data('sym')。如果要将值设置为属性,则可以使用 attr 方法。

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.

这篇关于使用jQuery将选择选项附加到HTML5数据属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆