English 中文(简体)
获取格式投入值的最佳途径
原标题:Best way to access form input values
  • 时间:2012-05-27 15:48:27
  •  标签:
  • jquery

我有以下两个变量。 我会将数据变量发送到服务器, 返回后, 想要访问各种原始值 。

我可以对数据或原始变量做某种eq(1).val()处理,但随后需要确定我是否正在以正确的顺序访问这些数据,这可能导致未来的维护问题。 或者我可以使用名称选择器从原始 DOM 获得数值, 但看起来我正在做两次工作。 或者我可以在原始表格中添加身份识别码和名称, 但是这似乎是一种浪费。

最理想的情况是, 我可以将窗体名称/ 值转换为某种对象, 但与序列式的 Array 不同, 它不会生成一个数组, 而是有与名称挂钩的值 。 例如, 如果我的窗体有 < code\ lt; input name= "foo" value= "bar" & gt; , 我可以用新的 Object.foo 访问该值 。

对于这样做的最佳方式有什么建议吗?

var raw=$("#form_id").find( input,select );
var data=raw.serializeArray();
最佳回答

您可通过直接访问表单来做到这一点:

var form = $("#form_id")[0];
console.log( form.foo.value );
问题回答
var formVals = [];
$( select, input ).each(function(i, v){
   formVals[$(this).attr( name )] = $(this).val();
})

我认为 jQuery 允许您选择每个输入( 甚至选择、 收音机和复选框, 而该输入框实际上不是一次性输入), 以序列化的Array () 构建该阵列 :

var object = $(":input").serializeArray();

From the jQuery docs here : http://api.jquery.com/serializeArray/ Of course, restrict that $(":inputs") to the tragetted form especially if you gave multiple forms in your document.

这应该能让你看到一个物体看起来像:

[ { name: "foo", value: "bar" }, { ... } ]

希望这能帮上忙

var raw=$("#form_id").find( input,select );

This is more efficient. This has been proved in the Jsperf Performance test here It shows the above method outperforms other test cases in almost all browsers. Though direct get method of var item = $(".buttonRef"); outperforms in some cases. Note:Ids are accessible faster than classes though it is not recommended in most of the cases. When it comes to efficiency of time when accessing form input values u might consider ids.

注参考

我不知道,如果我得到 你的问题正确。

如果我能正确理解你的问题,我会把输入和格式字段按顺序排列:

var result = $( form ).serializeArray();

然后获得个别物体:

result.ObjectName;




相关问题
getGridParam is not a function

The HTML: <a href="javascript:void(0)" id="m1">Get Selected id s</a> The Function: jQuery("#m1").click( function() { var s; s = jQuery("#list4").getGridParam( selarrrow )...

selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

jQuery cycle page with links

I am using the cycle plugin with pager functionality like this : $j( #homebox ) .cycle({ fx: fade , speed: fast , timeout: 9000, pager: #home-thumbs , ...

jquery ui dialog opens only once

I have a button that opens a dialog when clicked. The dialog displays a div that was hidden After I close the dialog by clicking the X icon, the dialog can t be opened again.

jConfirm with this existing code

I need help to use jConfirm with this existing code (php & Jquery & jAlert). function logout() { if (confirm("Do you really want to logout?")) window.location.href = "logout.php"; } ...

Wrap text after particular symbol with jQuery

What I m trying to do, is wrap text into div inside ll tag. It wouldn t be a problem, but I need to wrap text that appears particularly after "-" (minus) including "minus" itself. This is my html: &...

热门标签