JQuery以编程方式添加隐藏元素 [英] JQuery add hidden element to form programmatically

查看:80
本文介绍了JQuery以编程方式添加隐藏元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

现在我真的得到了。我有一个在页面加载时加载的表单。在我的jquery
'ready'函数中,我将隐藏到该表单中,如下所示:

This is really getting to me now.I have a form that loads when the page loads.In my jquery 'ready' function I append a hidden to that form like so :

$("<input id='thehidden' type='hidden' name='thehidden' value='hiddenval'>").appendTo("#MenuForm")

当我用firebug检查表单内容时,我可以看到添加了元素。

When I check the form content with firebug i can see the element is added.

["$(\"#MenuForm\").children()"] is [div, input#thehidden hiddenval]

到目前为止一切都很好。当我提交此表并试着再次阅读elemet时,我无法获得我添加的新隐藏值的值。

All good so far.When I submit this form and try and read the elemet again,i can't get the value of the new hidden val I added.

alert($('#thehidden').val())

未定义

任何帮助将不胜感激

推荐答案

您究竟何时尝试从#thehidden div读取值?当按下提交按钮或提交后页面重新加载?如果您不是每次在页面加载时都创建输入,那么下一页加载就不会存在。

When exactly are you trying to read the value from #thehidden div? When the submit button is pressed or when the page reloads after submit? If you don't create the input every time on page load, it's not going to be there the next page load.

我测试了你的代码,只是创建输入并在警报中读回值,它对我来说很好。 自行查看

I tested your code with just creating the input and reading the value back in an alert and it works fine for me. Check it out for yourself.

这篇关于JQuery以编程方式添加隐藏元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆