我有一个 html 表单,在从下拉列表中选择输入字段时,我想添加一些 html 输入字段。我正在使用 append()
但它实际上只是附加到 div 假设我有 2 个选项 1,2 在选择 1 时一些输入字段被添加到表单中,但在为选项 1 选择 2 个输入字段时保持不变。我可以使用什么另一种方法来代替附加?我真的错过了一些非常基本的东西,谷歌搜索了很多,但在这种情况下没有帮助。
jQuery('#category').change(function() {
if( $(this).val() == 1 ) {
// input fields to add for 1
}
if( $(this).val() == 4 ) {
// input fields to add for 4
}
});
最佳答案
为什么不首先在表单中创建所有输入字段并执行以下操作:
$(document).ready(function()
{
$("#inputfield1").hide();
$("#inputfield2").hide();
});
jQuery('#category').change(function() {
if( $(this).val() == 1 ) {
$("#inputfield1").show();
$("#inputfield2").hide();
}
if( $(this).val() == 4 ) {
$("#inputfield2").show();
$("#inputfield1").hide();
}
});
关于jquery - 使用 jquery 在选项选择的基础上添加新的表单输入字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12142024/