开发者

jquery convert serialized form inputs to xml string

开发者 https://www.devze.com 2023-02-09 09:04 出处:网络
I\'m trying to get all my form inputs and then create with them an xml string containing there values in order t开发者_如何转开发o send them via ajax.

I'm trying to get all my form inputs and then create with them an xml string containing there values in order t开发者_如何转开发o send them via ajax.

i want this :

<form id="formtest">
 <input type="text" name="test1" id="test1"/>
 <input type="text" name="test2" id="test2"/>
 <input type="text" name="test3" id="test3"/>
 <input type="text" name="test4" id="test4"/>
 <input type="text" name="test5" id="test5"/>           
</form>

to become:

<formtest>
  <test1></test1>
  <test2></test2>
  <test3></test3>
  <test4></test4>
  <test5></test5>
</formtest>

How can this be done?

Thank's In Advance.


First, create your XML document within jQuery:

var xml = $('<xmlBody></xmlBody>'); //<xmlBody> can be replaced with whatever tag is appropriate for your uses

Add values like such:

xml.find('xmlBody').first().append('<tag>'+data+'</tag>');

You may need a plugin to serialize the XML. This, perhaps?

This is a very simple example, but hopefully it will get you started. Look around the jQuery docs for more info.


    var xml = $('<formtest></formtest>');

$("form#formtest input").each(function(){
    xml.find('formtest').first().append('<' + $(this).attr('id') + '>' + $(this).val() + '</' +  $(this).attr('id') + '>');
});
0

精彩评论

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