【发布时间】:2018-05-29 16:35:53
【问题描述】:
我正在尝试查找与数组匹配的 texbox 值,我在下面提供了代码 sn-p:
$('#message').on('keyup', function () {
suggest_array_sample = [
{ array_val: "#{person1}" },
{ array_val: "#{person2}" },
{ array_val: "#{person3}" }
];
found_variable_array = [];
$.each(suggest_array_sample, function (key, value) {
console.log(value);
if ($.inArray(value, textbox_value)) {
console.log('found');
found_variable_array.push(value);
} else {
console.log('not found');
}
})
console.log(found_variable_array);
});
<textarea id="message"></textarea>
问题是它总是返回整个数组,而不仅仅是当我在文本框上键入 #{person1} 时输出应该是的匹配项
[{array_val:"#{person1}"}] //预期输出
[{array_val:"#{person1}"},{array_val:"#person2"}]// 在文本框中找到两个或多个匹配项时的预期输出
而不是
[{array_val:"#{person1}"},]{array_val:"#{person2}",{array_val:"#{person3}"}] //当前输出
这是否可以使用inArray() 或者我需要更改代码。
【问题讨论】:
-
也无法将对象与输入值进行比较,输入值是您尝试使用 $.inArray 执行的字符串
标签: javascript jquery