append()只在第一次工作..当在同一个div上附加相同的代码时

时间:2016-06-17 10:20:35

标签: javascript jquery

        $('.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>

1 个答案:

答案 0 :(得分:1)

这一行的问题:

$('#div'+ val_id).remove();


此代码正在从dom中删除内容。尝试删除div的html。用这个替换代码

$('#div'+ val_id).html('');