如何在使用jQueryUI时更改选择框选项
How to change a select box option when using jQueryUI
我想做的是:你打开页面,有一个选择框,你在其中选择选项a,另一个选择框,选项B,出现并显示选项取决于用户为a选择了什么选项
我在一些帮助下做到了这一点,但我想定制选择框,我能找到的唯一方法是实现jQueryUI来支持onchange回调。我可以让选项B出现,但它不会改变选项。
这是目前为止我写的(非常混乱的)代码:
<head>
<meta content='text/html; charset=utf-8' http-equiv='content-type' />
<title>Jailbreak</title>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
<link href="scripts/reset.css" rel="stylesheet" type="text/css" />
<link href="scripts/styles.css" rel="stylesheet" type="text/css" />
<link rel="Stylesheet" href="scripts/jquery-ui.css" type="text/css">
<link rel="Stylesheet" href="scripts/ui.css" type="text/css">
<script type="text/javascript" src="scripts/jquery-ui.js"></script>
<script type="text/javascript" src="scripts/ui.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$("select[name='select1']").selectmenu();
$("select[name='select2']").selectmenu();
});
</script>
</head>
<body>
<script>
function Select1(){
var phoneselect=document.getElementById("select1");
var phoneid = phoneselect.options[phoneselect.selectedIndex].value;
if( phoneid == 'p1' ){
document.getElementById( 'select2' ).length=0;
document.getElementById( 'select2' )[0]=new Option("1.1", "1.1", true, false);
document.getElementById( 'select2' )[1]=new Option("1.2", "1.2", false, false);
document.getElementById( 'select2' )[2]=new Option("1.3", "1.3", false, false);
}
else if(phoneid == 'p2' ){
document.getElementById( 'select2' ).length=0;
document.getElementById( 'select2' )[0]=new Option("1.2", "1.2", true, false);
document.getElementById( 'select2' )[1]=new Option("1.3", "1.3", false, false);
document.getElementById( 'select2' )[2]=new Option("1.4", "1.4", false, false);
}
$('#fwselector').show('slow', function() {
});
}
</script>
<div id="mainimg"><img src="images/devices.png"/></div>
<div id="formcontainer">
<form>
<select id="select1" name="select1" onchange="Select1();">
<option value="Phone" disabled="disabled" >Phone</option>
<option value="p1">iPhone 2G</option>
<option value="p2">iPhone 3G</option>
<option value="p3">iPhone 3GS</option>
<option value="p4">iPhone 4</option>
<option value="p5">iPhone 4S</option>
</select>
</form>
<div id="fwselector" style="display: none">
<select name="select2" id="select2">
<option value="Firmware" disabled="disabled" selected="selected">Firmware</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
</select>
</div>
</div>
</body>
</html>
我应该提到的是,除了HTML或CSS之外,我对其他任何东西都不太熟悉,我只是通过将不同指南的内容合并在一起来做到这一点。
谢谢。
这里我们可以做很多事情来简化。
// let's "cache" our selectors to make them fast
var $select1 = $("select[name=select1]")
var $select2 = $("select[name=select2]")
// lets create an object to store options based on value of select one
var options = {
"value1": [
{value: "sample1", text: "sample option #1"},
{value: "sample2", text: "sample option #2"},
{value: "sample3", text: "sample option #3"},
{value: "sample4", text: "sample option #4"},
]
"value2": [
{value: "sample5", text: "sample option #5"},
{value: "sample6", text: "sample option #6"},
{value: "sample7", text: "sample option #7"},
{value: "sample8", text: "sample option #8"},
]
}
$select1.change(function() {
var val = $(this).val()
$select2.empty(); // clear all options of $select2
populateSelectBox2(val)
})
// add a specific option
function addOption(text, value) {
$select2.append("<option>", {value: value, text: text})
}
// loop through possible answers and add each
function populateSelectBox2(val) {
for (var i = 0; i < options[val].length; i++) {
addOption(options[val][i].text, options[val][i].value);
}
}
这是你要找的吗?
http://www.appelsiini.net/2010/jquery-chained-selects相关文章:
- CKeditor:更改对话框中的默认选择选项
- 如何在从多选下拉列表中选择选项值时动态生成文本框
- AngularJs列表ng单击以选择选项转换
- 使用基于文本的 Jquery 选择选项
- 当选择值x时,Javascript需要在正确的位置显示文本区域,从而循环通过具有选择选项的表单
- 在php和mysql中选择选项.一旦选择了选项,就列出我的sql数据库数据
- 选择选项时如何显示警报
- 使用php动态更改选择选项
- 选择“选择选项”时显示文本.怎么做
- LEAFLET地图和带有选择选项的标记-ANGULARjs
- 使用js将动态内容添加到选择选项列表中
- 用于使用javascript循环选择选项
- AngularJS-选择选项并在函数中使用它
- 禁用/启用基于单选/选择选项所选Javascript的文本框
- 如何使用jQuery隐藏与选择选项相关的选择选项列表
- 选择选项是添加或删除事件
- 使用jQuery向“选择选项”添加默认属性
- 构建HTML选择字段并使用JavaScript数组选择选项
- 带有动态选择选项的Firefox html5验证异常
- 只有当Angular从资源对象检索数据时,才选择选项元素