添加和删​​除输入框

时间:2012-05-25 16:47:49

标签: jquery css

我试图根据勾选方框添加和删除输入框。 但是我似乎遇到了一些问题。我不确定的一个薄是如何将$ this(div)与其他属性链接起来。

HTML:

<form>
    Lifetime Secs <input id="sec" type="checkbox" name="sec" value="sec" /> <br />
    Lifetime KBytes <input id="kb" type="checkbox" name="kb" value="kb" />
</form>

JavaScript的:

$("#input1, #input2").click(function() {
    if ($("this:checked").length) {
        $(this).after('<input type="text" name="input" id="inputbox" />');
    } else {
        $('this input').remove();
    }
});

JSFiddle: http://jsfiddle.net/felix001/eA32C/22/

谢谢,

4 个答案:

答案 0 :(得分:1)

修复了一些问题..见下文,

$(function() {
    $('input:checkbox').change(function() {        
        if ($(this).is(':checked')) {
            $(this).after('<input type="text" name="input" id="inputbox"  />');
        } else {
            $(this).next('input').remove();
        }
    });
});

DEMO

答案 1 :(得分:0)

$(function() {
  $('input:checkbox').on('change', function() {
    if ( this.checked ) {
        $(this).after('<input type="text" name="input" id="inputbox"  />');
    } else {
        $(this).next('input').remove();
    }
  });
});

答案 2 :(得分:0)

$("#sec,#kb").click(function() {
    $this = $(this);

if ( $this.attr('checked')) {

    $this.after('<input type="text" name="input" id="inputbox"  />');

} else {

    $('#inputbox').remove();

}
});

http://jsfiddle.net/eA32C/39/

答案 3 :(得分:0)

可以在此处找到实现此目的的一种方法:http://jsfiddle.net/eA32C/40/