开发者

How to get input values of the 1st and Nth div below with jQuery?

开发者 https://www.devze.com 2023-01-31 01:15 出处:网络
$.fn.serialize() is close to what I want except that it only applies for <form>,how to do it for general <div> ?

$.fn.serialize() is close to what I want except that it only applies for <form>,how to do it for general <div> ?

<div>
<input type="text" name="var1" />
....
</div>

<div>
..
</dvi>

<div>
<input type="text" name="varN" />
....
</div开发者_StackOverflow>


jQuery's .serialize() method is not just for forms.

From the docs:

The .serialize() method can act on a jQuery object that has selected individual form elements, such as <input>, <textarea>, and <select>. However, it is typically easier to select the <form> tag itself for serialization:

So just select the elements you want, and use .serialize():

var ser = $('input[name=var1],input[name=varN]').serialize();


I believe the best answer, as karim79 put it in a comment, is:

$("div:eq(0), div:eq(n)").find(":input").serialize();


You can create a form, clone your divs inside that, and then call .serialize() on that new form

$f = $('<form/>');
$('div').each(function(i,e) {
  $f.append($(e).clone() )
})
$f.serialize();

But isn't it required by DTD for input to be wrapped in <form>?

0

精彩评论

暂无评论...
验证码 换一张
取 消