我想更改select标签中所选选项值的表格.但是我也希望将结果附加到现有的表行中,为此我能够将数据检索到表中但不能在tbody的内部html中追加新行只想知道如何将行追加到ajax响应文本我尝试过这个.
<script>
function resInvoice(InvoiceVal){
var xhttp;
if (InvoiceVal == "") {
document.getElementById("InvoiceBody").innerHTML = "";
return;
}
xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (xhttp.readyState == 4 && xhttp.status == 200) {
document.getElementById("InvoiceBody").innerHTML="<tr>"+xhttp.responseText+"</tr>");
}
};
xhttp.open("GET", "GetSelect.PHP?InData="+InvoiceVal, true);
xhttp.send();
}
</script>
<tbody id="InvoiceBody">
<tr id="select">
<td colspan="2">
<select class='form-control select2' onchange="resInvoice(this.value)">
<?PHP
$fqeyr="SELECT ItemID,LineItemName FROM `per_addnew_lineitem`";
$faddnew=MysqLi_query($con,$fqeyr);
while($fnrow=MysqLi_fetch_array($faddnew)){
$lname=$fnrow['LineItemName'];
?>
<option value="<?PHP echo $fnrow['ItemID'] ;?>">
<?PHP echo $lname ; ?>
</option>
<?PHP } ?>
</select>
</td>
<td>
<button type="button" class="btn btn-primary btn-xs" style="margin-top:12px;" data-toggle="modal" data-target="#AddNewLine">+ Add New Line Item </button>
</td>
</tr>
</tbody>
并在IN GetSelect.PHP
if(isset($_GET['InData'])){
$InData=$_GET['InData'];
$InDataRes=MysqLi_query($con,"SELECT * FROM `per_addnew_lineitem` where ItemID='$InData'");
while($InvoiceD=MysqLi_fetch_array($InDataRes)){
echo '<td>'.$InvoiceD['LineItemName'].'</td>' ;
echo '<td>'.$InvoiceD['Description'].'</td>' ;
echo '<td>'.$InvoiceD['Calculation'].'</td>' ;
}
}
解决方法:
试试这个
var new_row = "<tr>" + xhttp.responseText + "</tr>";
$("#InvoiceBody").append(new_row);
代替
document.getElementById("InvoiceBody").innerHTML="<tr>"+xhttp.responseText+"</tr>");