jquery选择器无法从隐藏字段读取

标签 jquery forms css-selectors element hidden

( answers aggregated into another question )

以下 jquery 1.3.2 代码有效:

<input type="select" value="236434" id="ixd" name='ixd' />

<script>
console.log( $('#ixd') );
console.log( $("input[name='ixd']") );
</script>

控制台显示:

[input#ixd 236434]

[input#ixd 236434]

但是,将输入设置为“隐藏”会阻止选择器工作。有什么线索吗?

<input type="hidden" value="236434" id="ixd" name='ixd' />

<script>
console.log( $('#ixd') );
console.log( $("input[name='ixd']") );
</script>

控制台显示:

[]

[]

最佳答案

不知道为什么会失败。我在工作中经常做同样的事情,无论表单域是否隐藏,它都有效。

也许试试这个:

<input type="hidden" value="236434" id="ixd" name='ixd' />

<script>
    console.log($("#xid").val())
</script>

这将为您提供隐藏字段的值。要从表单字段中获取值,需要使用 .val() 方法。

关于jquery选择器无法从隐藏字段读取,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/996644/

相关文章:

jquery - 带或不带引号的属性选择器?

javascript - WordPress 插件“订阅评论已重新加载”: Loading Scripts Only When it is Necessary

javascript - Jquery 选择值传递

javascript - 在页面加载时显示第一个 div 并隐藏其他,然后在单击菜单项时显示其余部分

javascript - 根据先前输入中输入的 HexColor 自动将输入更新为较暗的 HexColor

javascript 只输入字母

mysql - 需要使用当前事件用户名将来自 WordPress 的表单数据存储在数据库中

javascript - "too much recursion"在我的旋转横幅脚本中

CSS 特殊性和继承

javascript - 如何在没有 id 的情况下访问 canvas 元素?