开发者

Jquery - Accessing input element within a class by its index

开发者 https://www.devze.com 2023-03-06 04:11 出处:网络
I need some pointer here. I am trying to set the value of multiple input text within a class by its index value. The extra validate class is just for inline form validation and I think it is not the p

I need some pointer here. I am trying to set the value of multiple input text within a class by its index value. The extra validate class is just for inline form validation and I think it is not the problem.

I was able to access multiple check boxes within a class using the same method but I can't figure this one out. Can someone please give me a hint as to what I did wrong? Thank you.

Script

//--(4) Load Delivery Cost Input Box
var tmp开发者_如何学JAVAstr = $('#editDeliveryCost').html();
if (tmpstr != ""){
   var tmparray = tmpstr.split(',');
   for (i = 0; i < tmparray.length; ++i) {
      $('.typeDeliveryCost:eq('+(tmparray[i]-1)+')').val(tmparray[i]);
   }

HTML

<input type="text" name='cost[]' id='inp01' class="validate[required,custom[price]] typeDeliveryCost" />
<input type="text" name='cost[]' id='inp02' class="validate[required,custom[price]] typeDeliveryCost" />
<input type="text" name='cost[]' id='inp03' class="validate[required,custom[price]] typeDeliveryCost" />
<input type="text" name='cost[]' id='inp04' class="validate[required,custom[price]] typeDeliveryCost" />


change this

$('.typeDeliveryCost:eq('+(tmparray[i]-1)+')').val(tmparray[i]);

to

$('.typeDeliveryCost:eq(' + i + ')').val(tmparray[i]);


Shouldnt

$('.typeDeliveryCost:eq('+(tmparray[i]-1)+')').val(tmparray[i]);

be

$('.typeDeliveryCost:eq(' + i + ')').val(tmparray[i]);

:eq(index) takes a zero-based index (eq-selector)

0

精彩评论

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