$('.addvalue').change(function(){
val_id = this.id;
counter_btn++;
counter_inp++;
if( $('#'+val_id).val() == '1'){
$('#div'+ val_id).append('<input id="myInput'+counter_btn +'" class="form-control" required placeholder="Enter Text"
style="margin-top: 5px;border-color: green;border-width: medium;" type="text" />');
$('#div' +val_id).append(' <button type="button" id="mybtn'+counter_btn +'" class="btn btn-success button_class btn-xs" style="margin-top: 5px;" onclick="addtest('+counter_btn+')" >ADD</button>');
$("#myInput"+counter_btn).focus();
}else{
$('#div'+ val_id).remove();
}
});
<html>
<select class="form-control newclass addvalue"
name="CandInv-54" id="sugar_id">
<option value="">Select Value</option>
<option value="absent">ABSENT</option>
<option value="PRESENT(+++)">PRESENT(+++)</option>
<option value="1">ADD</option>
</select>
<div id="divsugar_id">
append code will go here...
</div>
</html>
答案 0 :(得分:1)
这一行的问题:
$('#div'+ val_id).remove();
此代码正在从dom中删除内容。尝试删除div的html。用这个替换代码
$('#div'+ val_id).html('');