javascript - Jquery 不将类标识为选择器
问题描述
我有两个 Jquery 函数,其中一个将在单击按钮时将新的动态行插入到表单中,而其他函数将验证该行中的单元格。
var currentItem = 11;
$('#addnew').click(function(){
currentItem++;
$('#items').val(currentItem);
var strToAdd = '<tr><td><select class="form-control select2 productSelect" name="product'+currentItem+'" id="product'+currentItem+'" style="width: 100%"> <option disabled selected value> -- select a Product -- </option>'+ <?php foreach ($productData as $product) { echo "'<option value=" . $product->product_id . ">" . $product->product_name . "</option>'+";}?> '</select> </td><td><input class="form-control quantitySelect" name="quantity'+currentItem+'" id ="quantity'+currentItem+'"type="text" /></td><td><input class="form-control" name="freeIssue'+currentItem+'" id="freeIssue'+currentItem+'" type="text" /></td> <td align="center"><button class="btn btn-danger" name="close" id="close" onclick="SomeDeleteRowFunction(this)"><i class="fa fa-close"></i></button></td></tr>';
$('#data').append(strToAdd);
});
这里我将类名productSelect作为第二个函数的标识符进行验证。但它无法识别该类,并且验证不适用于我通过上述函数添加到表单中的行。
$('.productSelect').change(function () {
var elementID = $(this).attr("id");
var numberPart = elementID.split("t", 2);
if ($(this).val() != null ) {
$('#quantity'+ numberPart[1]).prop('required',true);
}
});
这就是整个代码的样子
<script type='text/javascript'>
$(document).ready(function() {
$('.productSelect').change(function () {
var elementID = $(this).attr("id");
var numberPart = elementID.split("t", 2);
if ($(this).val() != null ) {
$('#quantity'+ numberPart[1]).prop('required',true);
}
});
var currentItem = 11;
$('#addnew').click(function(){
currentItem++;
$('#items').val(currentItem);
var strToAdd = '<tr><td><select class="form-control select2 productSelect" name="product'+currentItem+'" id="product'+currentItem+'" style="width: 100%"> <option disabled selected value> -- select a Product -- </option>'+ <?php foreach ($productData as $product) { echo "'<option value=" . $product->product_id . ">" . $product->product_name . "</option>'+";}?> '</select> </td><td><input class="form-control quantitySelect" name="quantity'+currentItem+'" id ="quantity'+currentItem+'"type="text" /></td><td><input class="form-control" name="freeIssue'+currentItem+'" id="freeIssue'+currentItem+'" type="text" /></td> <td align="center"><button class="btn btn-danger" name="close" id="close" onclick="SomeDeleteRowFunction(this)"><i class="fa fa-close"></i></button></td></tr>';
$('#data').append(strToAdd);
});
});
</script>
解决方案
使用 $(document).on.('change', '.productSelect', function (){}) 因为方法找不到文档准备好的类
推荐阅读
- django - 是否可以为 dockerized angular plus django web 应用程序创建一个 .msi 安装程序?
- postgresql - 使用 libpq 准备多行
- java - 如何使用 Java 8 收集器对三重嵌套地图求和
- python - 为什么我的树莓派只搜索 pypi 简单版本?
- amazon-web-services - 创建客户端或资源对象时必须指定区域?
- linux - 如何查找 zip 字符串是否存在于文件行中。如果找到解压 zip 文件
- quarkus - 来自 spring-cloud-config 的 Quarkus 可配置路径
- regex - 如果域不在 net、com 或 org 中,则正则表达式在 URL 中查找第一个目录
- amazon-web-services - 安装了 aws cli v2 的 AWS CodeBuild nodejs 映像
- javascript - 有没有办法将数据存储到 json 数组中,而无需删除 javascript 中 json 数组中先前存储的数据?