用jQuery替换空的选择框
我试图用一条短信替换所有空的选择框,但它不起作用。以下是我的jQuery:用jQuery替换空的选择框,jquery,select,replace,each,Jquery,Select,Replace,Each,我试图用一条短信替换所有空的选择框,但它不起作用。以下是我的jQuery: var s = $("table.preference select option"); if(s.length == 0){ s.parents("td").html("No more preferences to add!"); } 我也试过: $("table.preference select option").each(function(){ if($(this).length == 0){
var s = $("table.preference select option");
if(s.length == 0){
s.parents("td").html("No more preferences to add!");
}
我也试过:
$("table.preference select option").each(function(){
if($(this).length == 0){
$(this).parents("td").html("No more preferences to add!");
}
});
但两者都不起作用。我在两个不同的表中有两个select
标记-我需要jQuery找到任何空的select框并用文本消息替换其表单元格。有人知道我哪里出错了吗?试试这个:
$("table.preference select").each(function() {
if ($(this).children("option").length === 0) {
$(this).parents("td").html("No more preferences to add!");
}
});
根据@Kon的建议,如果您希望检查内部可能的optgroup
标记,您可以尝试以下方法:
$("table.preference select").each(function() {
if ($("option", this).length === 0) {
$(this).parents("td").html("No more preferences to add!");
}
});
这将检查选择
本身以及任何选项组
内的选项
这里的示例:试试这个:
$("table.preference select").each(function() {
if ($(this).children("option").length === 0) {
$(this).parents("td").html("No more preferences to add!");
}
});
根据@Kon的建议,如果您希望检查内部可能的optgroup
标记,您可以尝试以下方法:
$("table.preference select").each(function() {
if ($("option", this).length === 0) {
$(this).parents("td").html("No more preferences to add!");
}
});
这将检查选择
本身以及任何选项组
内的选项
这里的示例:尝试以下方法:
$("table.preference select").each(function(){
if($(this).children().length == 0) {
$(this).parents("td").html("No more preferences to add!");
}
});
试着这样做:
$("table.preference select").each(function(){
if($(this).children().length == 0) {
$(this).parents("td").html("No more preferences to add!");
}
});
你试过其他选择器吗?不经意间:
var s = $("table.preference select:empty");
if(s.length != 0){
...
}
你试过其他选择器吗?不经意间:
var s = $("table.preference select:empty");
if(s.length != 0){
...
}
如果在表中的所有select元素之一中至少有一个option元素,则第一种方法将不起作用
不使用$.each()的解决方案:
如果在表中的所有select元素之一中至少有一个option元素,则第一种方法将不起作用
不使用$.each()的解决方案:
这对我有用
- 检测任何带有空选项的选择框李>
- 筛选包含某些文本“abc”的任何div
这对我有用
- 检测任何带有空选项的选择框李>
- 筛选包含某些文本“abc”的任何div
你所说的选择框是什么意思?你是说?你确定你的
里面真的没有更多的
?这与带有空白文本/值的
不同抱歉,我应该指定它是标记标记。是的,我的PHP没有将任何选项
标记输入选择
。选择框是什么意思?你是说?你确定你的
里面真的没有更多的
?这与带有空白文本/值的
不同抱歉,我应该指定它是标记标记。是的,我的PHP没有将任何选项
标记输入选择
。无需筛选子项,因为选择
的唯一有效子项是选项
@Kon-optgroup
在选择中也是有效的。Karl,touche。。我忘了optgroup。虽然如果您有optgroup,那么您的代码将永远找不到任何选项,即使它们存在,因为children()
只在DOM树下移动一个级别。无需筛选子级,因为select
的唯一有效子级是option
@Kon-optgroup
在select.kall中也是有效的,触摸。。我忘了optgroup。虽然如果您有optgroup,那么您的代码将永远找不到任何选项,即使它们存在,因为children()
只会沿着DOM树向下移动一个级别。我喜欢这种方法-如果我错了,请纠正我,它是否也适用于$(table.preference select:empty)
@kaveman:yes,:如果里面真的什么都没有(没有textNodes,没有optgroup),empty
也可以工作。一个空格将打破:empty
我喜欢这种方法-如果我错了,请纠正我,它是否也适用于$(table.preference select:empty)
@kaveman:是的,:empty
也适用于内部没有任何内容的情况(没有文本节点,没有optgroup)。单个空格将中断:空