gpt4 book ai didi

选中的 JavaScript 复选框不起作用

转载 作者:行者123 更新时间:2023-11-30 12:22:33 25 4
gpt4 key购买 nike

代码:

var str = '2,4';
var accept_message = str.split(',');


var accept_message_html = '';
var accpet_message_Obj = {
1: 'Friends',
2: 'Models',
3: 'Premiums',
4: 'Basics'
};
$.each(accpet_message_Obj, function(key, value) {
accept_message_html += '<span style="padding-right:30px;"><input style="float:none;" type="checkbox" name="privacy_options[]"' + (key == accept_message[key] ? ' checked="checked"' : '') + ' value="' + key + '" /> ' + value + '</span>\n';
});

$('#content-area').html(accept_message_html);

以上编码无效。我需要 checkbox 检查哪些值是 24 但没有选中复选框。 :(

JSFIDDLE

最佳答案

您需要使用 indexOf()检查 key 是否存在于 accept_message

The indexOf() method returns the first index at which a given element can be found in the array, or -1 if it is not present.

代码

accept_message.indexOf(key) > -1

DEMO

然而,作为indexOf() IE9+支持,可以使用$.inArray()

Search for a specified value within an array and return its index (or -1 if not found).

代码

$.inArray(key, accept_message) > -1

关于选中的 JavaScript 复选框不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30500181/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com