javascript - each loop fail in jquery plugin? -
javascript - each loop fail in jquery plugin? -
can explain me happen here??
i have html code:
<div class="calculadora"> <div class="clear campos"> <label>amount</label> <input class="fi" id="amount" type="text" name="amount" value=""/> </div> <div class="clear campos"> <label>down payment</label> <input class="fi" id="downpay" type="text" name="downpay" value=""/> </div> <div class="clear campos"> <label>years</label> <input class="fi" id="years" type="text" name="years" value=""/> </div> <div class="clear campos"> <label>rate</label> <input class="fi" id="rate" type="text" name="rate" value=""/> </div> <input id="cal" type="button" value="cacular"/> <div class="result"></div> </div> and i'm making jquery plugin, need attr('value') of each input, , i´m doing way:
this.each(function(){ var obj = $(this), vacio = parsefloat( $('.fi', obj).attr('value')); // code... but happens it's first value of first input... why??
but!! if create way :
var s = $('.fi', obj).each(function(){ alert ($(this).attr('value')) }); it workss!!! why?? good???
tks in advance if can explain me.
.attr() returns value of specified attirbute first element in collection called upon.
from doc:
get value of attribute for first element in set of matched elements.
when create the other way, extracting value attribute info each element in matched set.
one way accomplish you're after done using .map() function, returns jquery array:
var vals = $('.fi', obj).map(function(){ homecoming this.value; }); to transform pure javascript array, utilize vals.toarray();
note: no need jquery value of input because this in event handlers , in iteration on elements current domelement, can this.value.
javascript jquery loops each
Comments
Post a Comment