如果选择列表包含特定值,JQUERY将自动隐藏该列表
如果选择列表包含xxx作为选项之一,如何使用jQuery隐藏该列表 可能吗 更新: 以上就是一个例子,ROB回答说,它确实有效。但在我的情况下,这个选择列表首先是由另一个jquery函数生成的 就是这样:如果选择列表包含特定值,JQUERY将自动隐藏该列表,jquery,Jquery,如果选择列表包含xxx作为选项之一,如何使用jQuery隐藏该列表 可能吗 更新: 以上就是一个例子,ROB回答说,它确实有效。但在我的情况下,这个选择列表首先是由另一个jquery函数生成的 就是这样: <select name="productsize" id="productsize"> <option value="">1</option> <option value="">xxx</option> </s
<select name="productsize" id="productsize">
<option value="">1</option>
<option value="">xxx</option>
</select>
//->
我试着这样做:
<script type="text/javascript"><!--
function GetAvailProductSizes() {
$('select#productsize option').remove();
$('select#productsize').append('<option value=""><? echo $langdata['oneprodpage_selectsize']; ?>...</option>');
var color = $('#productcolor').val();
if (color > 0) {
var availsizes;
var http_request = new XMLHttpRequest();
http_request.open( "GET", '<? echo ROOT; ?>/autocompleteavailsizes/?productid=<? echo $thisproduct['id']; ?>&color=' + color, true );
http_request.send(null);
http_request.onreadystatechange = function () {
if ( http_request.readyState == 4 ) {
if ( http_request.status == 200 ) {
availsizes = eval( "(" + http_request.responseText + ")" );
$('select#productsize').show();
for (var i = 0; i < availsizes.length; i++) {
$('select#productsize').append('<option value="' + availsizes[i].id + '">' + availsizes[i].name + '</option>');
};
} else {
alert( "There was a problem with the URL." );
}
http_request = null;
}
};
};
}
//->
但它不起作用
哦,是的,我的选择列表有样式显示:无相当直接
<script type="text/javascript"><!--
function GetAvailProductSizes() {
$('select#productsize option').remove();
$('select#productsize').append('<option value=""><? echo $langdata['oneprodpage_selectsize']; ?>...</option>');
var color = $('#productcolor').val();
if (color > 0) {
var availsizes;
var http_request = new XMLHttpRequest();
http_request.open( "GET", '<? echo ROOT; ?>/autocompleteavailsizes/?productid=<? echo $thisproduct['id']; ?>&color=' + color, true );
http_request.send(null);
http_request.onreadystatechange = function () {
if ( http_request.readyState == 4 ) {
if ( http_request.status == 200 ) {
availsizes = eval( "(" + http_request.responseText + ")" );
$('select#productsize').show();
$('select#productsize option').each(function() {
if ($(this).text() == '-') {
$('select#productsize').hide();
}
});
for (var i = 0; i < availsizes.length; i++) {
$('select#productsize').append('<option value="' + availsizes[i].id + '">' + availsizes[i].name + '</option>');
};
} else {
alert( "There was a problem with the URL." );
}
http_request = null;
}
};
};
}
或:
Rob回答的另一种方法是检查从该选择器中选择的元素数量
$('#productsize option').filter(function(){
var t = this.textContent || this.innerText;
return t === 'xxx';
}).parent().hide();
这假设您设置了每个选项的value属性,而不是内部文本。@user1978483是的,在两行之间读取,第一行隐藏它,第二行显示它。如果选项有值,这可能是更好的解决方案。在他的例子中,文本是xxx,而不是值。你的答案很好!!!谢谢你!但现在我明白了,我提出了一个错误的问题。我会更新我的问题
$('select:has(option:contains("xxx"))').hide();
$('#productsize option').filter(function(){
var t = this.textContent || this.innerText;
return t === 'xxx';
}).parent().hide();
if($('#productsize option[value="xxx"]').length) {
$('#productsize').hide();
}