2014-05-12 74 views
0

我想将一个字符串值放在一个数组中,根据它们相对于所有具有类名的元素的位置。我得到了我想要使用的索引数组,现在我想使用每个索引值来选择适当的值。如何从循环内部选择特定的jquery元素值?

$(function() 
{ 
    $("#date").datepicker(); 
    $('input[name=date]').datepicker(); 
    var dateInput = $("select[name='searchString']"); 
    var theClassTime = $("input.TextBoxDate"); 
    var checkedindex = []; 
    var classday = []; 

    $("input[name='selectedCourses']").each(function (i) 
    { 
     if (this.checked) 
     { 
      // this works fine 
      checkedindex.push(parseInt(i)); 

      // this is where I’m trying to select values based on index. 
      var dayofclass = $(".TextBoxDate:eq(" + checkedindex[i] + ")"); 

      classday.push(dayofclass); 

      alert(classday); 
     } 
    }); 
}); 

说checkedindex有值:2,3,9在checkedindex的索引0处的含义是2,在1 = 3和在2 = 9。我想用2,3和9做这样的事情:

var dayofclass = $(".TextBoxDate:eq(" + checkedindex[i] + ")"); 

我在做什么错在这里?

+0

你有没有试过循环选择的课程。然后在你的if语句中if(this.checked)var addVal = this.innerhtml(); classday.push(addval);. –

+0

我上面写的基本上是在选中的课程中获得教学输入的价值。存储该值然后将其添加到数组中。我不确定你是否试图从复选框列表中获取html或索引 –

回答

0

你不需要按ID查找复选框,你已经有了对它的引用。如果您需要元素,则可以使用this;如果您需要jquery对象,则可以使用$(this)

$(function() 
{ 
    $("#date").datepicker(); 
    $('input[name=date]').datepicker(); 
    var dateInput = $("select[name='searchString']"); 
    var theClassTime = $("input.TextBoxDate"); 
    var checkedindex = []; 
    var classday = []; 

    $("input[name='selectedCourses']").each(function (i) 
    { 
     if (this.checked) 
     { 
      // this works fine 
      checkedindex.push(parseInt(i)); 

      // this is where I’m trying to select values based on index. 
      // this will get the value of the current checkbox 
      var dayofclass = $(this).val(); 

      classday.push(dayofclass); 

      alert(classday); 
     } 
    }); 
}); 

的jsfiddle例如:http://jsfiddle.net/2TJLm/

是,基本上你想要做什么?

+0

请将您的答案放在这里,而不仅仅是它的链接 –

+0

var dayofclass = $(this).val();没有做正确的选择。我试图使用这个:$(“。TextBoxDate:eq(”+ checkedindex [i] +“)”)作为选择器。 – CloudyKooper

0

使事情更容易

只需使用一个数组来存储...

var selectedCourses = $("input[name='selectedCourses']"), 
    theClassTime = $("input.TextBoxDate"), 
    checkedindex = [], 
    go = true; 

function dothings() { 
    selectedCourses.each(function (i) {//for each possible selection 
     var cur = theClassTime.eq(i);//get current selected 

     //do something 
     if (this.checked) { 
      cur.addClass('red');//for checked 
      if (go) { checkedindex.push(i); }//store initial selections 
     } else { 
      cur.removeClass('red');//for non checked 
     } 
    }); 
    go = false;//turn off 
    alert(checkedindex);//show stored array 
} 

selectedCourses.on('change', function() { dothings(); });//run on event 

dothings();//run initially 

made a fiddle不知道有关标记被使用,所以它只是一个视觉表现

+0

这是我的场景:当用户登陆此页面时。有些箱子已经被检查过。我创建了一个数组来收集那些我成功做过的检查框的位置。现在,如果用户对窗体进行更改,在点击函数期间,我想使用先前保存的早期数组中的索引来完成一些工作。 – CloudyKooper

+0

没问题,更新了解决方案以便能够使用最初存储的数组。 – FiLeVeR10

+0

@CloudyKooper是否解决了您的问题? – FiLeVeR10

0

终于搞明白了。该代码允许我在页面加载时使用存储在数组中的数据,并在提交表单时进行比较。

<script type="text/javascript"> 


     $(function() { 

      $("#date").datepicker(); 
      $('input[name=date]').datepicker(); 
      var dateInput = $("select[name='searchString']"); 

      var checkedindex = []; 
      var classdate = []; 
      var classtime = []; 
      var dayofclass = []; 
    $("input[name='selectedCourses']").each(function (i) { 
     if (this.checked) { 

      checkedindex.push(parseInt(i)); 

     } 

    }); 
    for (var x = 0; x < checkedindex.length; x++) { 
     var ind = checkedindex[x]; 

     var dateofclass = $(".TextBoxDate:eq(" + ind + ")"); 
     var timeofclass = $(".TextBoxTime:eq(" + ind + ")"); 
     var classday = $("select[name='searchString']:eq(" + ind + ")"); 

     classdate.push(dateofclass); 
     classtime.push(timeofclass); 
     dayofclass.push(classday); 


     alert(classdate[x].val() + " " + classtime[x].val() + " " + dayofclass[x].val()); 
    } 
}); 
</script> 
+0

很高兴你知道了。目前还不清楚你最初想做什么...... – caspian

相关问题