javascript - 显示 div 何时获得选择或选项

标签 javascript jquery

我有一个带有信息页面和编辑页面的应用程序 信息页面显示信息。 在编辑页面中,我有带有选择框等的字段

我想对两者使用相同的代码。所以我尝试了

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/

相关文章:

javascript - 动画();在 Javascript 中卡住一秒钟

javascript - foo.substr 给出 "Is not a function"错误

javascript - VueJS2 和 Window 对象 - 如何使用?

javascript - 如何在不使用井号 (#) 的情况下通过 url 打开新页面并滚动到新页面的 div?

jquery mobile 如何将选择菜单向右移动

asp.net - 使用jquery将数据插入数据库

javascript - 如何停止函数处理直到ajax调用完成?

javascript - 使用 jQuery 设置 CSS 属性

javascript - 为什么我的未初始化变量可以工作?

javascript - jquery 在车速表中移动指针图像