下拉菜单上的多个属性选择器
multiple attribute selector on drop down menu?
我有一个下拉菜单,有多个选择。每个都有一个选项值。当其中三个被选中时,我应该在框的右侧显示一些文本。到目前为止,一切顺利。
我的问题是选择只有这三个显示文本和其余的显示什么。问题是我可以通过使用!== 'value1' && 'value2'
使其中两个工作,但我不知道只选择这三个的最佳方法。
<select id="award-type">
<option value="">choose one</option>
<option value="award1">Award 1</option>
<option value="award2">Award 2</option>
<option value="award3">Award 3</option>
<option value="award4">Award 4</option>
<option value="award5">Award 5</option>
<option value="award6">Award 6</option>
</select>
<div class="award-text"></div>
和JS:
function dropDownSelect() {
$('#award-type').on('change', function (e) {
var selectValue = $(this).val();
if (selectValue == 'award1') {
$('.award-text').show().text("you won award 1");
}
if (selectValue == 'award2') {
$('.award-text').show().text("you won award 2");
}
if (selectValue == 'award3') {
$('.award-text').show().text("you won award 3");
}
else if (selectValue !== 'award1' && 'award2' && 'award3') {
$('.award-text').show().text(" ");
}
});
}
任何想法都将非常感激!
首先这个条件不是正确的语法
selectValue !== 'award1' && 'award2' && 'award3'
应该是
selectValue !== 'award1' && selectValue !== 'award2' && selectValue !== 'award3'
第二,为什么你不写一个简单的elseif
而不是多个if语句
function dropDownSelect() {
$('#award-type').on('change', function (e) {
var selectValue = $(this).val(),
$text = $('.award-text'); // cache selector
if (selectValue == 'award1') {
$text.show().text("you won award 1");
} else if (selectValue == 'award2') {
$text.show().text("you won award 2");
} else if (selectValue == 'award3') {
$text.show().text("you won award 3");
} else {
$text.hide();
}
});
}
$('#award-type').on('change', dropDownSelect).change();
工作小提琴首先去除function dropDownSelect()
并替换为$(function() {
要解决这个错误,你必须这样做:
else if (selectValue != 'award1' && selectValue != 'award2' && selectValue != 'award3')
完整代码
$(function() {
$('#award-type').on('change', function(e) {
var selectValue = $(this).val();
if (selectValue == 'award1') {
$('.award-text').show().text("you won award 1");
}else if (selectValue == 'award2') {
$('.award-text').show().text("you won award 2");
}else if (selectValue == 'award3') {
$('.award-text').show().text("you won award 3");
}else{
$('.award-text').show().text(" ");
}
});
});
不要忘记包含jQuery库:
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js"></script>
你为什么不这样做呢:
$('#award-type').change(function() {
var award = $(this).find('option:selected').attr('data-award');
$('.award-text').show().text( award ? "you won award "+award : '' );
});
…并更改你的HTML,在每个选项上包含一个data-award属性:
<select id="award-type">
<option value="">choose one</option>
<option value="award1" data-award="1">Award 1</option>
<option value="award2" data-award="2">Award 2</option>
<option value="award3" data-award="3">Award 3</option>
<option value="award4" data-award="">Award 4</option>
<option value="award5" data-award="">Award 5</option>
<option value="award6" data-award="">Award 6</option>
</select>
<div class="award-text"></div>
为了简单起见,我个人建议:
$('#award-type').change(function(){
var self = $(this),
opt = self.find('option:selected'),
index = opt.index(),
v = opt.text();
self.next('.award-text').text(function(){
return index > 0 && index < 4 ? 'You won ' + v : '';
});
});
JS Fiddle demo.
引用:
-
change()
. -
find()
. -
index()
. -
next()
. -
text()
.
试试这个:它工作得很好:——
$('#award-type').on('change', function (e) {
var selectValue = $(this).val();
if (selectValue === 'award1' || selectValue ==='award2' || selectValue ==='award3') {
$('.award-text').show().text($('#award-type :selected').text());
}
else {
$('.award-text').show().text("nothing");
}
});
我认为最好的方法是使用切换结构,因为你可以使用多个条件(你也可以加入情况和/或玩休息),你也有一个-默认情况,正是你正在寻找的。
$('#award-type').change(function () {
switch ($('#award-type').val()) {
case "award1":
$('.award-text').text("you won award 1");
break;
case "award2":
$('.award-text').text("you won award 2");
break;
case "award3":
$('.award-text').text("you won award 3");
break;
default:
$(".award-text").text(" ");
}
return true;
});
小提琴:http://jsfiddle.net/egorbatik/WGFNA/8/
function dropDownSelect() {
$('#award-type').on('change', function (e) {
var selectValue = $(this).val();
if (selectValue != 'award1') {
$('.award-text').show().text("you won award 1");
}
if (selectValue == 'award2') {
$('.award-text').show().text("you' won award 2");
}
if (selectValue == 'award3') {
$('.award-text').show().text("you won award 3");
}
else if (selectValue !== 'award1' && 'award2' && 'award3') {
$('.awad-text').show().text(" ");
}
});
}
相关文章:
- jQuery最近父级的数据属性选择器
- 如何为HTML属性/选择器创建全局名称空间或变量
- 属性选择器不起作用(语法错误、无法识别的表达式)
- 为什么属性选择器不适用于jQuery
- JQuery中的CSS属性选择器
- Jquery $this属性选择器是否包含单词
- 使用 *= 的 jQuery 属性选择器
- 如何在jQuery中定义条件属性选择器
- 不区分大小写的 jQuery 属性选择器
- jQuery 查找不适用于属性选择器
- IE中的jQuery属性选择器
- 从属性选择器中提取值
- j查询 ID 属性选择器
- 使用本地存储值作为属性选择器
- 任务不可能:jQuery属性选择器
- Cheerio:SyntaxError:属性选择器格式不正确:对象全局
- 属性选择器未更改属性
- 类似regex的CSS属性选择器通配符捕获
- 下拉菜单上的多个属性选择器
- 互斥数据属性选择器