我有一个带有信息页面和编辑页面的应用程序 信息页面显示信息。 在编辑页面中,我有带有选择框等的字段
我想对两者使用相同的代码。所以我尝试了
if($('#product').has('option'))
{
console.log('hasSelect')
}
else{
console.log('NOSELECTNO')
}
在单个页面中没有选项或选择可用 但在编辑中它是。
我如何确保它能正常工作(以及为什么它不起作用)
编辑尝试了这个 2:
var attr = $('#product div').attr('select');
if (typeof attr !== typeof undefined && attr !== false) {
console.log("welmetselect")
}
else
{
console.log("zonderselect")
}
编辑:HTML
<div id= product>
<div>some more divs</div>
<div> in 1 of the div we have <select><option></option></select></div>
</div>
和 html 信息页
<div id= product>
<div>only information</div>
<div>only text </div>
</div>
最佳答案
我想你需要这样的东西:
if ($("#product option").length )
如果没有选项,长度将为 0(因此为 false)
关于javascript - 显示 div 何时获得选择或选项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46422407/