开发者

JQUery: Filter select elements from a .map query

开发者 https://www.devze.com 2023-03-09 16:15 出处:网络
I\'m sure this has been answered here but I just don\'t know how to word the question. I am getting all of the elements of a form using:

I'm sure this has been answered here but I just don't know how to word the question. I am getting all of the elements of a form using:

//get all form elements
$("#" + thisForm + " :input").map(function () { 

//get element data
var elementName = $(this).attr('name');
var elementType = $(this).attr('type');

and then storing them in a javascript object. The problem is that select and t开发者_如何学Pythonextarea elements do not have a type, so they appear as undefined. Is there a way to filter for them using the :input").map function or do I have to go to something like this:

$(thisForm + " > fieldset > select").add(thisForm + " > fieldset > textarea").each(function() {
  // do stuff
});

Thanks


If the returned elementType is undefined, you can try getting the tagName property (plain Javascript HTMLElement property) which will return the name of the tag itself (like select, textarea, etc.).

So you can do something like:

var elementType = $(this).attr('type');
if (typeof elementType == 'undefined') {
    elementType=this.tagName.toLowerCase();
}

jsFiddle Demo

0

精彩评论

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