tags:

views:

26

answers:

1

Hi,

Trying to send postdata thru ajax
The code tried so far,

 var $inputs = $('#myform :input');
 var values = {};
 $inputs.each(function(i,field) {


                      if($(this).is(':text'))
                      {
                             //alert("id : " + field.id + " value : "+ field.value);
                             values[field.id] = field.value
                      }
                      if($(this).is(":radio"))
                      {
                                var v = $(this).attr('checked')
                                if (v == true)
                                {
                                       values[this.name] = $(this).val()
                                       //alert($(this).attr('checked'))
                                }
                      }
                      if($(this).is(":checkbox"))
                      {
                           var v = $(this).attr('checked')
                           if (v == true)
                           {
                               values[this.name] = $(this).val()
                               //alert($(this).attr('checked'))
                           }
                      }
                      alert($(this).is(":hidden"))
                      if($(this).is(":hidden"))
                      {
                           values[field.name] = field.value
                      }
                      //alert("dropdownlist : " + $(this).is('option:selected'));
                      //values[field.id]  = field.value


                });

It works fine, but i want to determine whether dropdownlist is selected or not based
on it I need to add values[field.id] = field.value
Is there any way to find out about dropdownlist
Similar to $(this).is(':checkbox'),$(this).is(':text')
Any help will be valuable

+2  A: 

You can check if it's a <select> element, like this:

$(this).is("select")

Since .is() takes any selector, just use an element selector :)

From an overall perspective though, you seem to be replcating built-in functionality, take a look at .serialize() and .serializeArray(), you can use this to do what you're already trying, for example:

$.post(url, $("#myform").serialize());
Nick Craver
I have tried using serializeArray,But in some cases I need to use id of an elementUsing serialize array doesn't give me idof element . It gives name:value pair
tazim
Is there any possiblity of using serializeArrayand obtaining id also ?
tazim