尝试在ajax请求后获取隐藏值
我试图在加载ajax请求后获取包含json的隐藏表单值...
奇怪的是jquery看到刚刚通过ajax加载的隐藏值...但是它无法获取该值:
if ($("#json_nav").length) {
alert('element is there!');
var j = $("#json_nav").val();
alert(j); //is empty
}
我的#json_nav如下:
<input type='hidden' id='json_nav' value='{"c_type":3,"c1":"1","c2":"617","c3":"769"}'/>
我的代码正在处理无ajax请求...并且我已经确认隐藏值正在ajax请求中返回...但无法获取该值...
I am attempting to get a hidden form value which contains json, after the ajax request is loaded...
The odd thing is that jquery sees the hidden value that was just loaded via ajax... however it can't get the value:
if ($("#json_nav").length) {
alert('element is there!');
var j = $("#json_nav").val();
alert(j); //is empty
}
my #json_nav is as follows:
<input type='hidden' id='json_nav' value='{"c_type":3,"c1":"1","c2":"617","c3":"769"}'/>
My code is working with none ajax requests.... and i have confirmed that the hidden value is being returned in the ajax request... but can not get the value...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我的猜测是,页面上有一个
id
“json_nav”的不同元素,因此您正在检索第一个元素的值,而不是新元素的值。这是我的意思的一个例子:
Live copy
这是无效的(
id
值必须是独特),但这是一个很容易犯的错误。当您通过 ID 查找内容时,浏览器通常会给您第一个,但是当然不可能确定,因为浏览器可以自由地对无效文档执行它们想要的操作。My guess is that you have a different element on the page with the
id
"json_nav", and so you're retrieving the value of that first one, rather than the new one.Here's an example of what I mean:
Live copy
This is invalid (
id
values must be unique), but it's an easy mistake to make. Browsers will typically give you the first one when you look things up by ID, but of course it's impossible to be certain as browsers are free to do what they want with invalid documents.