在循环中使用.append()在<选择>基于所选择的选项
Using .append() in a loop to add text from an object after a <select> based on the selected option
我想在根据选择的选项选择了一个选项后,在下拉菜单下插入一个文本平静。我写的代码会完全按照我的意愿去做。但似乎应该有一种更快、更优雅的方式来处理我要做的事情。我在想循环,但循环让我有点困惑,我不确定我写的函数做错了什么。
这是我的原始工作代码:
$('#Total_Package_Pool_Size').change(function() {
$('#PS_Dyn_warn').empty();
$('#PS_Dyn_warn').append("This is the exact size of the pool, this is not an up to size.")
var PS_Warning_Obj = {
one : " A pool 14 x 28 is track space 14 and track length 29",
two : " A pool 16 x 32 is track space 16 and track length 33",
three: " A pool 18 x 34 is track space 18 and track length 35",
four : " A pool 20 x 40 is track space 20 and track lenght 41"
};
var PS_Warning = document.getElementById('PS_Dyn_warn');
var sizeDD = document.getElementById('Total_Package_Pool_Size');
var sizeOpt = sizeDD.options[sizeDD.selectedIndex].text;
switch (sizeOpt) {
case "14 x 28":
$('#PS_Dyn_warn').show();
$("#PS_Dyn_warn").append(PS_Warning_Obj.one);
break;
case "16 x 32":
$('#PS_Dyn_warn').show();
$("#PS_Dyn_warn").append(PS_Warning_Obj.two);
break;
case "18 x 36":
$('#PS_Dyn_warn').show();
$("#PS_Dyn_warn").append(PS_Warning_Obj.three);
break;
case "20 x 40":
$('#PS_Dyn_warn').show();
$("#PS_Dyn_warn").append(PS_Warning_Obj.four);
break;
}
});
这是我试图写的循环:
var PS_Warning_Obj = {};
PS_Warning_Obj['14x28'] = [" A pool 14 x 28 is track space 14 and track length 29"];
PS_Warning_Obj['16x32'] = [" A pool 16 x 32 is track space 16 and track length 33"];
PS_Warning_Obj['18x36'] = [" A pool 18 x 34 is track space 18 and track length 35"];
PS_Warning_Obj['20x40'] = [" A pool 20 x 40 is track space 20 and track lenght 41"];
function Dynamic_PS_Warning() {
$('#PS_Dyn_warn').empty();
$('#PS_Dyn_warn').append("This is the exact size of the pool, this is not an up to size.")
var sizeDD = document.getElementById('Total_Package_Pool_Size');
var sizeOpt = sizeDD.options[sizeDD.selectedIndex].text;
var PS_Warning = document.getElementById('PS_Dyn_warn');
var psArray = PS_Warning_Obj[sizeOpt];
if (psArray) {
for (var i = 0; i < psArray.length; i++) {
for (x in PS_Warning_Obj) {
if (psArray === sizeOpt) {
PS_Warning.append(PS_Warning_Obj[x];
}
}
}
}
}
类似的东西更动态,只需将数字插入字符串
$('#Total_Package_Pool_Size').change(function () {
var sizeOpt = $('#Total_Package_Pool_Size option:selected').text(),
arr = sizeOpt.split('x').map(function(x) {
return +$.trim(x);
}),
text1 = "This is the exact size of the pool, this is not an up to size.",
text2 = "A pool " + sizeOpt + " is track space " + arr[0] + " and track length " + (arr[1] + 1)
$('#PS_Dyn_warn').text(text1 + text2).show();
});
FIDDLE
您可以通过删除消息的硬编码并使其动态化,使其更加灵活。此外,您还将jQuery与香草JavaScript混合在一起。你没有充分利用jQuery。
$('#Total_Package_Pool_Size').change(function() {
var dimensions = $(this).val().split('x');
var space = parseInt(dimensions[0]);
var length = parseInt(dimensions[1]);
var message = 'This is the exact size of the pool, this is not an up to size.';
message += ' A pool ' + space + ' x ' + length + ' is track space ' + space + ' and track length ' + (length + 1);
$('#PS_Dyn_warn')
.show()
.text(message);
});
这使用了select选项的值,而不是文本,这要好得多。您的HTML应该如下所示:
<select id="Total_Package_Pool_Size">
<option value="14x28">14 x 28</option>
<option value="16x32">16 x 32</option>
<option value="18x36">18 x 36</option>
<option value="20x40">20 x 40</option>
</select>
Fiddle
相关文章:
- Div根据<选择>菜单
- 如何更改<选择>使用angularJS从控制器获得的值
- 依赖<选择>HTML和JavaScript菜单
- 如何传递相同页面<选择>值到同一页<f: param>
- 渲染<选项值=“;foo”;选择>在服务器上
- <选择>标签不起作用
- 当我有<选择>下拉列表
- 记住<选择>重新加载页面
- <中的链接;选择>下拉选项
- 映射多个<选择>至<输入>
- <上的applyBindings();选择>正在导致我的subscribe()起火,
- 如何动态生成X数量的新<输入>基于a<选择>(Angular2内)
- 在<选择>使用angular ng选项可能使用ng init
- 如何在提交<选择>与AngularJS
- 使用动态<选择>以显示<输入类型=“;文本>
- 什么's导致我的<选择>框在选中时闪烁
- 点击<选择>元素,但不在<选项>
- 使<选择>选项”;点击“;自动地
- 如何动态修改<选择>在实体化css框架中
- Javascript:更改<选择>java脚本中的选项