动态地将带有bind onclick事件的html元素添加到其他动态添加的html元素中

时间:2017-09-18 09:07:59

标签: javascript jquery html bind

我知道这个问题的一部分已经被问过,我按照它的答案但是它仍然不起作用(添加的html元素没有显示),你能帮帮我吗? 我希望标签和条目仅在选择下拉菜单选项时显示

这是html部分:

<select id="dropDown">
    <option value='1'>Doesn't repeat</option>
    <option value='2' id="repeatChange">Daily</option>
    <option value='3' id="repeatChange">Weekly</option>
    <option value='4' id="repeatChange">Biweekly</option>
    <option value='5' id="repeatChange">Monthly</option>
</select>
<label id="labelHowMuch">How many times ?</label>
<input type="text" id="repeatHowMuch" class="text ui-widget-content ui-corner-all"/>

这是jquery部分:

$('#labelHowMuch').remove();
$('#repeatHowMuch').remove();
$(document).on('click', '#repeatChange' , function() {
    $('#labelHowMuch').append($('#dropDown'));
    $('#repeatHowMuch').append($('#labelHowMuch'));
});

提前致谢!

1 个答案:

答案 0 :(得分:1)

这是你犯很多错误的正确方法:

$('#labelHowMuch').hide();
$('#repeatHowMuch').hide();
$(document).on('change', '#dropDown' , function() {
    if($(this)[0].value != 1){
        $('#labelHowMuch').show();
        $('#repeatHowMuch').show();
    }
    else{
        $('#labelHowMuch').hide();
        $('#repeatHowMuch').hide();    
    }
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select id="dropDown">
    <option value='1'>Doesn't repeat</option>
    <option value='2'>Daily</option>
    <option value='3'>Weekly</option>
    <option value='4'>Biweekly</option>
    <option value='5'>Monthly</option>
</select>
<label id="labelHowMuch">How many times ?</label>
<input type="text" id="repeatHowMuch" class="text ui-widget-content ui-corner-all"/>