jquery - 未定义值获取带有 id 作为数组的文本框
问题描述
我必须从文本框中获取值,其名称为数组。但我得到未定义的响应。
<input id="text[0][0]" type="text" value="" class="form-control label text-center" placeholder=" 1" style="">
<button>Text Box Value</button>
我试过jquery代码
var button = $("button")
button.on("click", function(){
var cell= $("#text[0][0]").val()
alert(cell)
}))
解决方案
您可以使用\\
在 jquery 选择器中转义方括号
var button = $("button")
button.on("click", function() {
var cell = $("#text\\[0\\]\\[0\\]").val()
alert(cell)
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input id="text[0][0]" type="text" value="1">
<button>Text Box Value</button>
注意 document.getElementById('text[0][0]')
在不转义方括号的情况下有效
推荐阅读
- spring - 如何使用 Response 对象返回状态码?
- javascript - 尽管使用了 localStorage,但主题切换器不会通过刷新持续存在
- webpack - 如何使用文件加载器在 next.js 中加载 SVG 图像?
- python - 如何在保存到磁盘之前更改 Scrapy 抓取的网页的一部分?
- flutter - 无法在颤动中添加垂直线
- celery - 等待 celery 组中的所有任务完成或出错
- javascript - FCM 发送一批消息(设备组)
- javascript - 尝试使用 forEach 方法转换数组中的单词并将它们输出到页面上,但每个条目都会覆盖前一个条目?
- java - Swing GridBagLayout:将第二行的按钮居中
- javascript - 在 javascript 的文本中显示用户名和密码值