无法使用jQuery设置隐藏的输入字段的值 [英] Can't set hidden input field's value using jQuery

查看:98
本文介绍了无法使用jQuery设置隐藏的输入字段的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在表单标签内有一个简单的输入字段:

I have a simple input field inside a form tag:

<body>
  <form action="#">
      <label>Input</label>
      <input type="hidden" id="foo" name="foo" />

  </form>
</body>

我试图通过js文件设置此值:

I tried to set this value from a js file:

$(document).ready(function(){
    $('#foo').val('foo')
})

,但是在html源中根本没有设置该属性.如果我尝试将输入类型设置为按钮"或其他任何内容,那么它将起作用.我只是用hidden输入字段无法做到这一点.我在做什么错了?

but in the html source the attribute isn't set at all. If I try to set the input type to "button" or anything else, it works. I just can't do it with a hidden input field. What am I doing wrong?

推荐答案

如果设置了val标签,是否可以从隐藏字段中检索值?

Can you retrieve the value from the hidden field if you set the val tag?

例如

<input type="hidden" id="foo" name="foo" value="bar" />

$(document).ready(function(){
  alert($('#foo').val());
})

这篇关于无法使用jQuery设置隐藏的输入字段的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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