我有一个变量selectedTagNames,它是这样设置的:
var selectedTagNames = $(".js-tag-name:checked").map(function(){return $(this).val()});
selectedTagNames
=> ["song:age_suitability=11-13", "song:age_suitability=5-7"]显示是一个数组。我想将这些值加入到一个字符串中,其中包含“财政”,但我做不到:
var searchTerm = selectedTagNames.join('||')
=> VM7234:2 Uncaught TypeError: undefined is not a function
message: "undefined is not a function"
stack: (...)get stack: function () { [native code] }
set stack: function () { [native code] }__proto__: Error我可以手动完成,而不是使用变量:
["song:age_suitability=11-13", "song:age_suitability=5-7"].join('||')
=> "song:age_suitability=11-13||song:age_suitability=5-7"因此,该变量的值必须与我在第二个示例中使用的数组不同,尽管它看起来是相同的。它们都只是返回typeof()的“object”。
我使用的是jquery版本1.7.1。
谁能告诉我a)这是怎么回事?( b)如何将所选复选框的值收集到数组中,以便我可以看到有多少复选框,并将它们连接在一起,从而避免出现任何问题?
发布于 2017-09-05 10:05:38
$(selector).map()返回类似数组的对象,类似于jQuery从选择器返回的内容。
相反,您需要静态方法:
$.map($('.js-tag-name:checked'), function(el) {
return $(el).val();
});发布于 2017-09-05 11:13:10
.map()返回一个jQuery集合,而不是一个数组。可以通过对结果调用.get()将其转换为数组。
var selectedTagNames = $(".js-tag-name:checked").map(function(){
return $(this).val()
}).get();https://stackoverflow.com/questions/46052239
复制相似问题