I am trying ot figure how to use jQuery to get the value of html attribute of many elements. My webpage updates dynamically using ajax. I have an attribute called number for in element in t开发者_如何转开发he part that is updated. I want to use the attribute value from each element so that i can use that data as parameters to a php file link. I have come across jquery's .attr() function, but it only seems to take the attribute value of the first element if finds. But what I want to do is get the attribute value for each element so that when I click on that element its corresponding attribute value is sent as parameters to the php file.
Thanks
you can combine attr() with .each()
method.
e.g.
$("div").each(function(){
$(this).attr("number");
});
Disclaimer: This most likely does not respond to the OP question (after re-reading), but will stay for some time in case it fill some need of the OP.
Use the .map()
method
var numbers = $('[number]').map(function(){
return $(this).attr('number');
});
this will create an array filled with the number
attribute of all the elements that have one.
Inside a click
handler (or any event handler), this
will refer to that element, for example:
$("#content").delegate("a", "click", function() {
alert($(this).attr("something")); //alerts "something" for the <a> you clicked
});
In this case we're using .delegate()
because you said "My webpage updates dynamically using ajax", so just attach the handler to a parent element that's not replaced via AJAX and it'll work for all elements you add beneath...in the example above we're binding to all <a>
elements, but just change the selector accordingly.
精彩评论