2017-02-14 3 views
-1
<table id="tbl" class="tbl1"> 
    <tr> 
    <th> 
     mobileno 
    </th> 
    <td> 
    </td> 
    </tr> 

</table> 

ich in der Lage einfügen bin bestimmte Werte zu erhalten, indem for-Schleife, aber ich bin nicht in der Lage append bereits gesetzt diese spezifischen Werte in spezifischen td id.I eine ID von td in HTML-Tabelle.aber ich bin in der Lage, nur in der ersten Zeile anzuhängen und dann für die zweite Zeile fügt es diese weiteren spezifischen Werte nicht in die nächste Zeile ein. wie Array-Werte in spezifische td id einfügenwie Array-Werte in spezifische td id

+0

Sie $ versucht ("# mo _" + i) statt $ ("# mo_1") ?? –

+0

Was ist das folio() wo es definiert ist? – Parithiban

+0

Sind Sie besonders daran interessiert, wie Sie Ihr Javascript geschrieben haben? Ich kann mir ein paar einfachere Wege vorstellen, um dies zu erreichen, indem ich dynamischere Funktionen zur Verfügung stelle. – Inkdot

Antwort

0

Try this statt Array:

var intArr = [111, 201, 345, 434, 532, 677, 790, 890, 989, 118, 107, 136, 125, 153, 142]; 

Und

$("#mo_" + i).append(arrfo); 
0

Verwendung

$("#mo_" + i).append(intmo[j]); 
0

Probieren Sie diese

var arrfo = []; 
 
$(document).ready(function() { 
 
    var intArr = [111, 201, 345, 434, 532, 677, 790, 890, 989, 118, 107, 136, 125, 153, 142 ]; 
 
    populatevalues(intArr); 
 
}); 
 

 

 
function populatevalues(intArr) { 
 
    for (i = 0; i < 6; i++) { 
 
    for (j = i * 3; j < 3; j++) { 
 
     var arrfo = intArr[j]; 
 
     $("#mo_"+j).append(arrfo); 
 
    } 
 
    } 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> 
 
<table id="tbl" class="tbl1"> 
 
    <tr> 
 
    <th> 
 
     mobileno 
 
    </th> 
 
    <td id="mo_0"> 
 
    </td> 
 
    </tr> 
 
    <tr> 
 
    <th> 
 
     mobileno 
 
    </th> 
 
    <td id="mo_1"> 
 
    </td> 
 
    </tr> 
 
    <tr> 
 
    <th> 
 
     mobileno 
 
    </th> 
 
    <td id="mo_2"> 
 
    </td> 
 
    </tr> 
 
    <tr> 
 
    <th> 
 
     mobileno 
 
    </th> 
 
    <td id="mo_3"> 
 
    </td> 
 
    </tr> 
 
    <tr> 
 
    <th> 
 
     mobileno 
 
    </th> 
 
    <td id="mo_4"> 
 
    </td> 
 
    </tr> 
 
    <tr> 
 
    <th> 
 
     mobileno 
 
    </th> 
 
    <td id="mo_5"> 
 
    </td> 
 
    </tr> 
 
</table>

+0

auf diese Weise funktioniert es nicht wo verwendet # mo + j.das nur Einfügen für die erste Zeile und meine HTML-Seite ist die Art, wie ich hvg code.dont macht die erste Zelle als mo_0 – ziel

+0

var mo_id = $ ("# tbl td") .attr ('id'); $ ("# mo _" + i) .app (a); Ich möchte auf diese Weise – ziel

+0

nach nur Ihrer Schleife kann ich 0 sein. – Parithiban

0

Update: Das ist jetzt nichts, wie Sie Ihren Code, aber es wird ermöglicht es Ihnen, dynamisch Elemente aus strm Array einfügen in <td></td> ABER die td muss Klasse mo haben - wie so <td class="mo" id="mo_1"></td> Sie die Ids halten kann du hast. Sie stören den Code nicht.

Eine Erklärung: Ich benutze die jQuery.each() "$.each(function(){})" -Funktion, um durch jedes HTML-Element, das 'class = "mo" hat, durch Einfügen der entsprechenden Strm-Array-Element, wie die Funktion fortschreitet, bis es keine gibt mehr mo's in der Objektliste. Auf diese Weise können Sie so viele Elemente mit den Elementen mo und strm hinzufügen, wie Sie möchten, ohne irgendeinen jquery-Code ändern zu müssen.

$(document).ready(function() { // encapsulate all functions in document ready function 
 

 
    // this is the strm array, in order to have a proper array you must have values inside "value" separated by , 
 
    var strm = ["111", "201", "345", "434", "532", "677", "790", "890", "989", "118", "107", "136", "125", "153", "142"]; 
 
    // execute mo, pass strm array to function. 
 
    mo(strm); 
 

 
    // mo function with array as input value 
 
    function mo(array) { 
 
     // jQuery.each() method. 
 
     // pass array as input value. 
 
     $.each(array, function(i, val) { // << index and value of each item in strm array 
 
     // if i is less than or equal to the number of elements with class mo, 
 
     if (i <= $(".mo").length) { 
 
      // get element with class mo equal to strm array index, insert value 
 
      $(".mo").get(i).append(val); 
 
     } 
 
     }); 
 
    } 
 
    });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<table id="tbl" class="tbl1"> 
 
    <tr> 
 
    <th> 
 
     mobileno 
 
    </th> 
 
    <td class='mo' id="mo_0"> 
 
    </td> 
 
    </tr> 
 
    <tr> 
 
    <th> 
 
     mobileno 
 
    </th> 
 
    <td class='mo' id="mo_1"> 
 
    </td> 
 
    </tr> 
 
    <tr> 
 
    <th> 
 
     mobileno 
 
    </th> 
 
    <td class='mo' id="mo_2"> 
 
    </td> 
 
    </tr> 
 
    <tr> 
 
    <th> 
 
     mobileno 
 
    </th> 
 
    <td class='mo' id="mo_3"> 
 
    </td> 
 
    </tr> 
 
    <tr> 
 
    <th> 
 
     mobileno 
 
    </th> 
 
    <td class='mo' id="mo_4"> 
 
    </td> 
 
    </tr> 
 
    <tr> 
 
    <th> 
 
     mobileno 
 
    </th> 
 
    <td class='mo' id="mo_5"> 
 
    </td> 
 
    </tr> 
 
</table>

+0

eigentlich Wenn meine statische Array mehr als 15 Werte, so möchte ich diese mehr als 15 Werte in dynamische Zeile der gleichen HTML-Tabelle anhängen. Deshalb verwende ich for loop hier für den Zweck der Einfügung Array-Werte mit weniger als oder gleich 15 Werte in HTML-Tabelle. wo jede Zeile kann mindestens 1 Wert und maximal 3 Werte in jeder Zeile der HTML-Tabelle – ziel

+0

Okay, ich habe meine Antwort zu aktualisieren eine vollkommen gute dynamische Einfügefunktion mit jQuery. Ich hoffe es passt zu deinen Bedürfnissen und du lernst etwas daraus! :) – Inkdot

Verwandte Themen