jQuery的分裂()和的indexOf结果"对象不支持此属性或方法" [英] jquery split() and indexOf results in "Object doesn't support this property or method"

查看:339
本文介绍了jQuery的分裂()和的indexOf结果"对象不支持此属性或方法"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下的code:

var selected = $('#hiddenField').val().split(",");
...
if (selected.indexOf(id) > 0) {
   ... set value ...
}

我动态创建的CheckBoxList,并试图通过将选定的ID添加到隐藏字段记住复选框的状态。

I'm dynamically creating a CheckBoxList, and trying to remember the state of the checkboxes by putting the selected IDs into the hidden field.

我得到一个错误,指出对象不支持此属性或方法。我的假设是,选择的是一个数组,它应该支持的indexOf。那是不正确的?

I get an error stating that "Object doesn't support this property or method". My assumption is that selected is an array, which should support indexOf. Is that incorrect?

推荐答案

有一个jQuery的方法来克服缺乏中的indexOf(),你可以使用的 .inArray() 来代替:

There's an jQuery method to overcome the lack of indexOf(), you can use .inArray() instead:

var selected = $('#hiddenField').val().split(",");
if ($.inArray(id, selected) > -1) {
   ... set value ...
}

jQuery.inArray()存在因为这个原因......如果你jQuery的,包括已经,没有必要再编写函数。注意:这实际上返回一个数字,如的indexOf()

jQuery.inArray() exists for just this reason...if you're including jQuery already, no need to write the function again. Note: This actually returns a number, like indexOf() would.

这篇关于jQuery的分裂()和的indexOf结果"对象不支持此属性或方法"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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