2014-05-12 41 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

相關問題