我们可以在HTML的下拉列表中选择分组吗? [英] Can we select group in drop down list in HTML?
问题描述
我想在下拉列表中以组的形式显示一些数据.为了在下拉列表中选择完整的组,我们可以选择一个组而不是组成员吗?如果是,则如何使用;如果否,则可以使用任何其他方式或其他控件?
I want to show some data in the form of groups in drop down list. Can we select a group instead of a group member in order to select full group in drop down list. If yes then how, if no then any other way or ant other control which can be used?
谢谢阿什瓦尼(Ashwani)
Thanks Ashwani
推荐答案
如果您的意思是可以在表单的 select
框中收集选项的子集,那么可以,我相信您可以:
If you mean can you collect a sub-set of options within a select
box of a form, then yes, I believe you can:
<form method="post" action="" enctype="form/multipart">
<fieldset>
<select>
<optgroup label="numbers">
<option>One</option>
<option>Two</option>
<option>Three</option>
</optgroup>
<optgroup label="letters">
<option>a</option>
<option>b</option>
<option>c</option>
</optgroup>
</select>
</fieldset>
</form>
demo at: http://jsbin.com/iwada3/edit
在意识到我是个白痴,并解决了真正问到的问题后才编辑(很抱歉,漫长的一天...):
Edited after realising I'm a moron, and addressing the question that was really asked (I'm sorry, long day...):
<!DOCTYPE html>
<html>
<head>
<script class="jsbin" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(
function() {
$('option.all').click(
function() {
$(this).parent().children().attr('selected','selected');
$(this).attr('selected','');
}
);
}
);
</script>
<meta charset=utf-8 />
<title>JS Bin</title>
<!--[if IE]>
<script src="http://html5shiv.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
<style>
article, aside, figure, footer, header, hgroup,
menu, nav, section { display: block; }
</style>
</head>
<body>
<form>
<fieldset>
<select multiple>
<optgroup class="num" label="numbers">
<option class="num all">Select all:</option>
<option class="num">One</option>
<option class="num">Two</option>
<option class="num">Three</option>
</optgroup>
<optgroup class="let"label="letters">
<option class="let all">Select all:</option>
<option class="let">a</option>
<option class="let">b</option>
<option class="let">c</option>
</optgroup>
</select>
</fieldset>
</form>
</body>
</html>
它确实需要jQuery(至少在此版本中为jQuery),但是对于您的要求,我看不到JS的替代方法.
It does require jQuery (in this version, at least), but I can't see an alternative to JS for what you require, I'm sorry to say.
如果您还有其他意思,那么我不确定我是否理解这个问题.
If you mean something else, then I'm not sure I understand the question.
这篇关于我们可以在HTML的下拉列表中选择分组吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!