开发者

How to validate ToCheckList jQuery plugin (or multiple select fields)?

开发者 https://www.devze.com 2022-12-20 01:40 出处:网络
Anyone ever use the toChecklist jQuery plugin? It basically turns a multi-select box into a list of c开发者_开发知识库heckboxes. Makes it easier for users to understand that they can choose more than

Anyone ever use the toChecklist jQuery plugin? It basically turns a multi-select box into a list of c开发者_开发知识库heckboxes. Makes it easier for users to understand that they can choose more than one item - excellent plugin.

However, I'm trying to make sure that users on my site pick at least 1 item in the list. ToChecklist has the capability of limiting the selection to a max value, but not minimum.

I'm already using the jQuery validate plugin to validate the whole form, any ideas to get it to work against a multi-select box (which would then hopefully just work with toChecklist)?

Thanks in advance!


The .toCheckList() still leaves the original <select> options intact, just hidden. This gives you the selected count using the :selected selector:

If you called $("#myList").toCheckList(), the code to get selected count would be:

$("#myList :selected").length

Used like:

if($("#myList :selected").length > 0)
  alert("Some options are selected!");
else
  alert("Oh snap! better pick something");


You can use the length property to see how many elements there are in the array. (Every jQuery object is also an array)

$(anything).length

http://api.jquery.com/length/

if ($("#mychecklist").length > 0)
{
  // valid - has 1 or more.
}
else
{
  // invalid - has 0 items.
}
0

精彩评论

暂无评论...
验证码 换一张
取 消