gpt4 book ai didi

javascript - 单击时使用 javascript 添加内容

转载 作者:行者123 更新时间:2023-11-30 20:59:01 24 4
gpt4 key购买 nike

尝试使用 javascript 添加一行 onClick。但它没有添加!

尝试了下面的代码:

    $(document).ready(function(){
var cont = 3
var qty = "qty"
var item = "item"
var what = "service"
$(".add-row").click(function(){
var nameqty = qty+cont
var nameitem = item+cont
var namewhat = service+cont
var markup = '<tr><td><select name="' + nameitem + '" class="country"><option>Select Item</option><option value='ZZ2017TF11A1'>Shirt</option><option value='ZZ2017TF11A2'>Pant</option></select></td><td><select class="country" name="' + namewhat + '"></select></td><td><input type="text" name="' + nameqty + '" value="" placeholder="Quantity"></td></tr>';
$(markup).insertBefore($('button[type="submit"]').closest("tr"));
cont++;
console.log(qty)
console.log(item)
console.log(what)
});
});
<form method="post" action="invoice_form.php" id="item_sel">

<table id="chiru_inv" class="table table-striped table-hover table-bordered table-responsive">
<tr>
<td colspan="3">
<input type="text" name="customer" value="" placeholder="Customer Name">
</td>
</tr>
<tr>
<th>Item</th>
<th>Service</th>
<th>Qty</th>

</tr>

<tr>
<td>
<select name='item1' class='country'>
<option value="Select Item">Select Item</option>
<option value='ZZ2017TF11A1'>Shirt</option><option value='ZZ2017TF11A2'>Pant</option></select></td>

<td><select class="country" name="service1">
</select></td>
<td><input type="text" name="qty1" value="" placeholder="Quantity"></td>
</tr>
<tr>
<td>
<select name='item2' class='country'>
<option>Select Item</option>
<option value='ZZ2017TF11A1'>Shirt</option><option value='ZZ2017TF11A2'>Pant</option></select></td>

<td><select class="country" name="service2">
</select></td>
<td><input type="text" name="qty2" value="" placeholder="Quantity"></td>
</tr>
<tr>
<td colspan="3"><button type="submit" name="btnsave" class="btn btn-default">
<span class="glyphicon glyphicon-save"></span> &nbsp; Save
</button>
</td>
</tr>
</table>
<input type="button" class="add-row" value="Add Row">
</form>

尝试使用 javascript 添加一行 on-click。但它没有添加!

我找不到我在上面的代码中犯了什么错误!

请查看代码片段,基本上是“JavaScript”部分。

最佳答案

你的代码有两个问题:

  1. 您的服务变量未定义。
  2. 您在 markup 变量中存在引号问题。

如果您修复了这两个问题,您的代码片段应该会像下面的代码片段所示那样工作。

我建议使用模型 div ,它将在您的 JS 中用于创建新项目,例如:

<div id="markup_model" class="hide">
<table>
<tr>
<td>
<select name="' + nameitem + '" class="country">
<option>Select Item</option>
<?php
$stmt=$user_home->runQuery("SELECT * FROM item ORDER BY Sr ASC ");
$stmt->execute();

if( $stmt->rowCount() > 0 ){
while( $row=$stmt->fetch( PDO::FETCH_ASSOC ) ){
echo "<option value='{$row['IRN']}'>{$row['Name']}</option>";
}
}
?>
</select>‌
​</td>
<td>
<select class="country" name="' + namewhat + '"></select>
</td>
<td>
<input type="text" name="' + nameqty + '" value="" placeholder="Quantity"/>
</td>
</tr>
</table>
</div>

然后在您的 JS 代码中,标记将如下所示:

var markup = $('#markup_model table').html();

希望这对您有所帮助。

$(document).ready(function() {
var cont = 3
var qty = "qty"
var item = "item"
var what = "service"

$(".add-row").click(function() {
var nameqty = qty + cont;
var nameitem = item + cont;
var namewhat = what + cont;

var markup = $('#markup_model tbody');

$(markup).find('.country:eq(0)').attr('name', nameitem);
$(markup).find('.country:eq(1)').attr('name', namewhat);
$(markup).find('input').attr('name', nameqty);

$(markup.html()).insertBefore($('button[type="submit"]').closest("tr"));

cont++;
});
});
<link href="https://netdna.bootstrapcdn.com/bootstrap/3.0.0/css/bootstrap.min.css" rel="stylesheet" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<form method="post" action="invoice_form.php" id="item_sel">
<table id="chiru_inv" class="table table-striped table-hover table-bordered table-responsive">
<tr>
<td colspan="3">
<input type="text" name="customer" value="" placeholder="Customer Name">
</td>
</tr>
<tr>
<th>Item</th>
<th>Service</th>
<th>Qty</th>
</tr>
<tr>
<td>
<select name='item1' class='country'>
<option value="Select Item">Select Item</option>
<option value='ZZ2017TF11A1'>Shirt</option><option value='ZZ2017TF11A2'>Pant</option></select></td>

<td><select class="country" name="service1">
</select></td>
<td><input type="text" name="qty1" value="" placeholder="Quantity"></td>
</tr>
<tr>
<td>
<select name='item2' class='country'>
<option>Select Item</option>
<option value='ZZ2017TF11A1'>Shirt</option><option value='ZZ2017TF11A2'>Pant</option></select></td>
<td><select class="country" name="service2">
</select></td>
<td><input type="text" name="qty2" value="" placeholder="Quantity"></td>
</tr>
<tr>
<td colspan="3"><button type="submit" name="btnsave" class="btn btn-default hide">
<span class="glyphicon glyphicon-save"></span> &nbsp; Save
</button>
</td>
</tr>
</table>
<input type="button" class="add-row" value="Add Row">
</form><br><br><br>

<div id="markup_model" class="hide">
<table>
<tr>
<td>
<select name="nameitem" class="country">
<option>Select Item</option>
</select>
</td>
<td>
<select class="country" name="namewhat"></select>
</td>
<td>
<input type="text" name="nameqty" value="" placeholder="Quantity" />
</td>
</tr>
</table>
</div>

关于javascript - 单击时使用 javascript 添加内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47313864/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com