将两个数组与jquery匹配,并为value和data

编程入门 行业动态 更新时间:2024-10-07 18:21:52
将两个数组与jquery匹配,并为value和data-attribute之间的匹配添加一个属性(Match two arrays with jquery and add an attribute to the matching between value and data-attribute)

尝试使用jQuery在选定选项中的数组中找到数据值,找到它时,我想添加一个属性,因为它们是选项标记和选定属性。

这看起来很容易,但实际上并非如此。

我的代码如下:

function selected_element() { var association = $("#association").children("option").map(function() { return this.value; }).get(); var ass_val = $("#association").children("option"); console.log(ass_val, association); var ass_data = $("#association").children("option").attr('data-selected'); console.log(ass_data ); if (ass_val == ass_data) { $("#association").children("option").val(ass_data).attr("selected","selected"); } }; selected_element();

这仅仅是一个例子:

我的第一行将映射我的数组,所以我返回值(1,2,3,4)。

我的第二行将返回选项和值,但我不能对他们做很多事情。

我的ass_data实际上是我用来在这种情况下检索数据标签的数组(数据选择)。

我最后一行代码仅仅是我想要实现的一个例子:

if (ass_val == ass_data) { $("#association").children("option").val(ass_data).attr("selected","selected"); } };

它不会工作,只是为了展示我的想法。

Try to find a data-value in an array in a selected option with jquery, and when find it, I want to add an attribute, since they're an option tag, a selected attribute.

It seems pretty easy but actually it is not.

My code is the following:

function selected_element() { var association = $("#association").children("option").map(function() { return this.value; }).get(); var ass_val = $("#association").children("option"); console.log(ass_val, association); var ass_data = $("#association").children("option").attr('data-selected'); console.log(ass_data ); if (ass_val == ass_data) { $("#association").children("option").val(ass_data).attr("selected","selected"); } }; selected_element();

This is just an example:

My first line will map my array, so I return the value (1,2,3,4).

My second line will return both options and values, but I can't do really a lot with them.

My ass_data is actually the array that I'm using to retrieve the data tag in this case (data-selected).

My last line of code is just an example of what I wish to achieve:

if (ass_val == ass_data) { $("#association").children("option").val(ass_data).attr("selected","selected"); } };

It won't work, is just to display my idea.

最满意答案

假设这些值是唯一的,您可以为下拉列表添加一个事件侦听器,然后find() :selected option :

$("#association").on("change", function () { var t = $(this), val = t.val(); if (val) { // filter out empty/null values as possible matches t.find(":selected").attr({<put attribute name here> : <attribute value here>}); // for example // t.find(":selected").attr({"data-selected" : true}); } });

Assuming the values are unique you could just add an event listener for the dropdown and then find() the :selected option:

$("#association").on("change", function () { var t = $(this), val = t.val(); if (val) { // filter out empty/null values as possible matches t.find(":selected").attr({<put attribute name here> : <attribute value here>}); // for example // t.find(":selected").attr({"data-selected" : true}); } });

更多推荐

本文发布于:2023-08-02 09:08:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1372678.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数组   并为   两个   jquery   data

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!