$(function() {
var index = 1;
$("#addcon").on("click", function() {
num = index + 1;
$("table.contact").append(`
<tr>
<td class='label'><label class='required'>Contact ${num}</label></td>
<td>:</td>
<td><select name=contact[${index}][type] class='contact' required>
<option style='display: none;' value='' selected>Select Type</option>
<option>Mobile</option>
<option>Landline</option>
<option>Email</option>
<option>Other</option>
</select></td>
<td><input type='text' name=contact[${index}][way] maxlength='300' class='way' required></td>
</tr>
`);
index++;
});
$("#remcon").on("click", function() {
if(index - 1 >= 1) {
$("table.contact tr").last().remove();
index--;
}
else {
alert("One is must!");
}
});
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<table class="contact">
<tr class="legend">
<td colspan="6">CONTACT DETAILS</td>
</tr>
<tr>
</tr>
</table>
上面是我的联系方式表格中的表格。用户可以通过单击“+”链接输入任意数量的联系人,并通过单击“-”链接删除。但为了验证数据,我需要根据各自选择框(组合框)中的选择更改输入字段的“类型”。有没有办法使用 JQuery 做到这一点?
示例:如果我在选择框中选择电子邮件,则与此选择框相对应的输入字段类型应更改为电子邮件类型。
弑天下
相关分类