如何使用jQuery清除SELECT输入中的所有选定项目? [英] How to clear all selected items in a SELECT input using jQuery?

查看:73
本文介绍了如何使用jQuery清除SELECT输入中的所有选定项目?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我认为这应该相当容易,但挣扎了一点......我有一个 SELECT 输入元素,允许用户选择多个项目。我想提供一种机制让他们清除所有选定的项目,可能只是一个 SPAN 标签或类似的东西。



无论如何...使用jQuery,我将如何清除 SELECT 输入元素上的选择,

> .val() 函数需要/返回一个数组,所以你可以简单地传入一个空数组来清除选择,如下所示:

  $(#selectID)。val []); 

您可以在这里测试


I am think this should be fairly easy, but struggling a bit ... I have a SELECT input element that allows the user to select multiple items. I would like to provide a mechanism for them to clear all selected items, probably will just be a SPAN tag or something similar.

Anyway ... using jQuery, how I would I clear the selection on the SELECT input element so no items are selected.

解决方案

In the case of a <select multiple> the .val() function takes/returns an array, so you can simply pass in an empty array to clear the selection, like this:

$("#selectID").val([]);

You can test it out here.

这篇关于如何使用jQuery清除SELECT输入中的所有选定项目?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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