尝试在ajax请求后获取隐藏值

发布于 2024-11-04 09:16:45 字数 434 浏览 0 评论 0原文

我试图在加载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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

旧时光的容颜 2024-11-11 09:16:45

我的猜测是,页面上有一个 id“json_nav”的不同元素,因此您正在检索第一个元素的值,而不是新元素的值。

这是我的意思的一个例子:

<div id='theText'>A div with the id "theText"</div>
<input type='text' id='theText' value='a field with the id "theText"'>

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:

<div id='theText'>A div with the id "theText"</div>
<input type='text' id='theText' value='a field with the id "theText"'>

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文