如何从< input>中检索值使用jQuery? [英] How to retrieve a value from <input> using jQuery?

查看:81
本文介绍了如何从< input>中检索值使用jQuery?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须隐藏输入字段,例如:

I have to hidden input fields such as:

<input name="foo" value="bar">
<input name="foo1" value="bar1">

我想检索这两个值并使用jQuery将它们发送到服务器。如何使用jQuery选择器引擎来获取这些值?

I'd like to retrieve both of those values and POST them to the server using jQuery. How does one use the jQuery selector engine to grab those values?

推荐答案

因为foo和foo1是输入字段的名称,您将无法使用jQuery(#)的id选择器,但您必须使用属性选择器:

As "foo" and "foo1" are the name of you input fields, you will not be able to use the id selector of jQuery (#), but you'll have to use instead the attribute selector :

var foo = $("[name='foo']").val();
var foo1 = $("[name='foo1']").val();

这不是性能方面的最佳选择。你最好设置输入字段的id并使用id选择器(例如$(#foo))或者至少为属性选择器提供一个上下文:

That's not the best option, performance-wise. You'd better set the id of your input fields and use the id selector (e.g. $("#foo")) or at least provide a context to the attribute selector:

var form = $("#myForm"); // or $("form"), or any selector matching an element containing your input fields
var foo = $("[name='foo']", form).val();
var foo1 = $("[name='foo1']", form).val();

这篇关于如何从&lt; input&gt;中检索值使用jQuery?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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