iterate through form elements in javascriptjquery - Stack Overflow

I am trying to iterate through all form elements with id that begins with a specified prefix and create

I am trying to iterate through all form elements with id that begins with a specified prefix and create an xml string with results, but it is getting a bit plicated as forms form input types seem to have different behaviors . Does this functionality already javascript, jQuery or third party jQuery module?

 function fnPreIterate(){                
         var XMLstring;
         $(':input[id*="f1"]').each(function() {             
            XMLstring += (" <" +this.name+ '>' + this.value + "</"  + this.name + "> " );       
        });         
        $('#XMLstring').html("<pre>" + fnEncodeEntities(string) + "</pre>");
};

I am trying to iterate through all form elements with id that begins with a specified prefix and create an xml string with results, but it is getting a bit plicated as forms form input types seem to have different behaviors . Does this functionality already javascript, jQuery or third party jQuery module?

 function fnPreIterate(){                
         var XMLstring;
         $(':input[id*="f1"]').each(function() {             
            XMLstring += (" <" +this.name+ '>' + this.value + "</"  + this.name + "> " );       
        });         
        $('#XMLstring').html("<pre>" + fnEncodeEntities(string) + "</pre>");
};
Share Improve this question edited Jul 6, 2011 at 10:13 bob asked Jul 6, 2011 at 9:59 bobbob 851 gold badge2 silver badges5 bronze badges
Add a ment  | 

5 Answers 5

Reset to default 2

If you use:

$(this).val() 

instead of

this.value

you'll save a lot of headaches with difference in form elements.

Another way to iterate is to use .serializeArray():

$.each($('#form').serializeArray(), function() {             
            string += (" <" +this.name+ '>' + this.value + "</"  + this.name + "> " );      
        });

Hope this helps. Cheers

PS: To select by prefix, you should do $(':input[id^="f1"]') (use ^ instead of *)

Use $(this).val() to get the value.

Additionally you mixed up XMLString and string which results in your code creating a global variable and failing when it's called a second time.

Using jQuery you should try:

function fnPreIterate(){                
  var XMLstring='';
  $(':input[id^="f1"]').each(function() {             
    var e = $(this), name=e.attr('name'), val=e.val();
    XMLstring += (" <" +name+ '>' + val + "</"  + name + "> " );      
  });         
  $('#XMLstring').html("<pre>" + fnEncodeEntities(XMLstring) + "</pre>");
};

I think it should work

You could make use of jQuery's serializeArray.

What is the problem you are facing? try using $(this) selector in your function instead. something like this $(this).attr('name') and $(this).val()

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744292682a4567110.html

相关推荐

  • iterate through form elements in javascriptjquery - Stack Overflow

    I am trying to iterate through all form elements with id that begins with a specified prefix and create

    7天前
    40

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信