2017-09-01 1 views
1

Folowing ist mein Code. was perfekt funktioniert. aber das Problem ist, wenn ich Knopf addiere, die neue hinzugefügte Auswahlwahl hat Wert mit Namen, den ich mit Identifikation benötige.
So wie kann ich ID an übergeben option.value = array [i];.JAVASCRIPT So erhalten Sie mehr als einen Wert in Wählen Sie Option

var array = ["VERPACKUNG & LOADING", "TASCHE GODOWN", "MRP", "HAUS HALTEN"];

ich kodieren SQL-Daten zu JSON. Dies ist Get mit PHP json_encode.

Kurz gesagt, ich brauche Id statt Wert in der Auswahlbox.

function addBillField (argument) { 
 
    var myTable = document.getElementById("myTable"); 
 
    var currentIndex = myTable.rows.length; 
 
    var currentRow = myTable.insertRow(-1); 
 
    
 
    var ip1 = document.createElement("select"); 
 
    
 
    //THIS VALUE CAME FROM PHP 
 
    var array = ["PACKING & LOADING","BAG GODOWN","MRP","HOUSE KEEPING"]; 
 
    
 
    //DONT REMOVE THIS SPACE 
 
    ip1.setAttribute("name", "billcategory" + currentIndex); 
 
    ip1.setAttribute("class", "form-control"); 
 
    ip1.name="billcategory[]"; 
 

 
    //Create and append the options 
 
    myTable.appendChild(ip1); 
 
    
 

 
    for (var i = 0; i < array.length; i++) { 
 
     var option = document.createElement("option"); 
 
     option.value = array[i]; 
 
     option.text = array[i]; 
 
     ip1.appendChild(option); 
 
    } 
 

 
    var ip2 = document.createElement("input"); 
 
    ip2.setAttribute("name", "billnumber[]" + currentIndex); 
 
    ip2.setAttribute("class", "form-control"); 
 

 
    var ip3 = document.createElement("input"); 
 
    ip3.setAttribute("name", "billamount[]" + currentIndex); 
 
    ip3.setAttribute("class", "form-control"); 
 

 
    var ip4 = document.createElement("input"); 
 
    ip4.setAttribute("name", "billgst[]" + currentIndex); 
 
    ip4.setAttribute("class", "form-control"); 
 

 
    var ip5 = document.createElement("input"); 
 
    ip5.setAttribute("name", "billtotal[]" + currentIndex); 
 
    ip5.setAttribute("class", "form-control"); 
 

 
    var addRowBox = document.createElement("input"); 
 
    addRowBox.setAttribute("type", "button"); 
 
    addRowBox.setAttribute("value", "Add More"); 
 
    addRowBox.setAttribute("onclick", "addBillField();"); 
 
    addRowBox.setAttribute("class", "btn"); 
 

 
    var currentCell = currentRow.insertCell(-1); 
 
    currentCell.appendChild(ip1); 
 
    currentCell.colSpan = 2; 
 

 
    currentCell = currentRow.insertCell(-1); 
 
    currentCell.appendChild(ip2); 
 

 
    currentCell = currentRow.insertCell(-1); 
 
    currentCell.appendChild(ip3); 
 

 
    currentCell = currentRow.insertCell(-1); 
 
    currentCell.appendChild(ip4); 
 

 
    currentCell = currentRow.insertCell(-1); 
 
    currentCell.appendChild(ip5);    
 

 
    currentCell = currentRow.insertCell(-1); 
 
    currentCell.appendChild(addRowBox);    
 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<table class="table webtbl" id="myTable"> 
 
    <thead> 
 
     <tr align="left" style="background-color: grey;color: white;"> 
 
      <th colspan="7"><span>BILL DETAILS:</span></th> 
 
     </tr> 
 
     <tr> 
 
      <th colspan="2" width="25%">Bill Category</th> 
 
      <th width="18%">Bil No.</th> 
 
      <th width="18%">Bill Amount</th> 
 
      <th width="18%">G.S.T</th> 
 
      <th width="18%">Total</th> 
 
      <th width="3%"></th> 
 
     </tr> 
 
    </thead> 
 

 
    <tbody> 
 
     <tr> 
 
      <td colspan="2"> 
 
       <select class="form-control" name="billcategory[]"> 
 
        <option value="">Select Bill Category</option> 
 
        <?php 
 
         $qury6 = $conn->query("select * from bill_category where status = 1"); 
 
         while($get_bil_cat = $qury6->fetch_array()){ 
 
          echo "<option value='$get_bil_cat[0]'>$get_bil_cat[1]</option>"; 
 
          } 
 
        ?> 
 
       </select> 
 
      </td> 
 
      <td> 
 
       <input type="text" class="form-control" name="billnumber[]" placeholder="Bill Number"> 
 
      </td> 
 
      <td> 
 
       <input type="text" class="form-control" name="billamount[]" placeholder="Bill Amount"> 
 
      </td> 
 
      <td> 
 
       <input type="text" class="form-control" name="billgst[]" placeholder="Bill GST"> 
 
      </td> 
 
      <td> 
 
       <input type="text" class="form-control" name="billtotal[]" readonly="" placeholder="000"> 
 
      </td> 
 
      <td align="center"> 
 
       <input type="button" class="btn btn-default" value="Add" onclick="addBillField();"> 
 
      </td> 
 
     </tr> 
 
    </tbody> 
 
</table>

+0

https://developer.mozilla.org/en-US/docs/Web/HTML/Element/select - 'wählen multiple' – aynber

+0

Dank aber dies ist nicht im Zusammenhang mit meiner Lösung –

+0

Ich verstehe nicht, was Möchten Sie, dass Ihre Auswahl angezeigt wird? – Lanrex

Antwort

0

Ich nehme zweite Variable mit Wert von ID. Und drucken Sie es in Optionswert. es funktioniert.

for (var i = 0; i < array.length; i++) { 
      var option = document.createElement("option"); 
      **option.value = array[i]; 
      option.text = array2[i];** 
      ip1.appendChild(option); 
     } 
1

Sie verwenden PHP-Variablen in der echo-Anweisung an der Linie echo "<option value='$get_bil_cat[0]'>$get_bil_cat[1]</option>";. In diesem Fall interpretiert PHP die Variablen als String. Daher sollten Sie die Variablen verketten, um Werte anzuzeigen. Verwenden Sie echo "<option value=".$get_bil_cat[0].">".$get_bil_cat[1]."</option>";.

Verwandte Themen