如果未选中单选按钮,jQuery将删除元素(jQuery remove an element if radio button is not checked)

如果选中相应的单选按钮,我有以下代码将元素附加到文档。 这工作正常,但如果未选中单选按钮,我需要一种方法来删除元素。

这是我到目前为止的情况?

// for each radio button $('input[type=radio]').each(function(){ // if it is checked if($(this).is(":checked")) { var cur = $(this).val(); // append an input text element only if it does not already exist if(!$('#characterList input:text').is(function(i,e){ return e.value==cur && e.name=='data['+cur+']'; })) { $('#characterList').append('<input type="text" name="data[' + $(this).val() + ']" value="' + $(this).val() + '" />'); } } else { // otherwise remove element if radio button not checked $('#characterList').remove('<input type="text" name="data[' + $(this).val() + ']" value="' + $(this).val() + '" />'); } }); });

.remove(...)什么都不做。 我在这做错了什么?

I have the following code that append elements to the document if the corresponding radio button is checked. This works fine, however I need a way to remove the elements if the radio button is NOT checked.

Here is what I have so far?

// for each radio button $('input[type=radio]').each(function(){ // if it is checked if($(this).is(":checked")) { var cur = $(this).val(); // append an input text element only if it does not already exist if(!$('#characterList input:text').is(function(i,e){ return e.value==cur && e.name=='data['+cur+']'; })) { $('#characterList').append('<input type="text" name="data[' + $(this).val() + ']" value="' + $(this).val() + '" />'); } } else { // otherwise remove element if radio button not checked $('#characterList').remove('<input type="text" name="data[' + $(this).val() + ']" value="' + $(this).val() + '" />'); } }); });

The .remove(...) does nothing at all. What am I doing wrong here?

最满意答案

remove()不能像那样工作。 您必须提供一个选择器来选择要删除的元素。 这可以这样做:

$('#characterList').remove('input[value="' + $(this).val() + '"]');

或者,或者:

$('#characterList').find('input[value="' + $(this).val() + '"]').remove();

此外,不是使用$(this).is(":checked")和$(this).val() ,而是使用:

if ( this.checked )

和:

var cur = this.value;

remove() doesn't work like that. You have to provide a selector to select the element you wish to remove. This can be done like so:

$('#characterList').remove('input[value="' + $(this).val() + '"]');

Or, alternatively:

$('#characterList').find('input[value="' + $(this).val() + '"]').remove();

Furthermore, instead of using $(this).is(":checked") and $(this).val(), you can simply use:

if ( this.checked )

And:

var cur = this.value;如果未选中单选按钮,jQuery将删除元素(jQuery remove an element if radio button is not checked)

如果选中相应的单选按钮,我有以下代码将元素附加到文档。 这工作正常,但如果未选中单选按钮,我需要一种方法来删除元素。

这是我到目前为止的情况?

// for each radio button $('input[type=radio]').each(function(){ // if it is checked if($(this).is(":checked")) { var cur = $(this).val(); // append an input text element only if it does not already exist if(!$('#characterList input:text').is(function(i,e){ return e.value==cur && e.name=='data['+cur+']'; })) { $('#characterList').append('<input type="text" name="data[' + $(this).val() + ']" value="' + $(this).val() + '" />'); } } else { // otherwise remove element if radio button not checked $('#characterList').remove('<input type="text" name="data[' + $(this).val() + ']" value="' + $(this).val() + '" />'); } }); });

.remove(...)什么都不做。 我在这做错了什么?

I have the following code that append elements to the document if the corresponding radio button is checked. This works fine, however I need a way to remove the elements if the radio button is NOT checked.

Here is what I have so far?

// for each radio button $('input[type=radio]').each(function(){ // if it is checked if($(this).is(":checked")) { var cur = $(this).val(); // append an input text element only if it does not already exist if(!$('#characterList input:text').is(function(i,e){ return e.value==cur && e.name=='data['+cur+']'; })) { $('#characterList').append('<input type="text" name="data[' + $(this).val() + ']" value="' + $(this).val() + '" />'); } } else { // otherwise remove element if radio button not checked $('#characterList').remove('<input type="text" name="data[' + $(this).val() + ']" value="' + $(this).val() + '" />'); } }); });

The .remove(...) does nothing at all. What am I doing wrong here?

最满意答案

remove()不能像那样工作。 您必须提供一个选择器来选择要删除的元素。 这可以这样做:

$('#characterList').remove('input[value="' + $(this).val() + '"]');

或者,或者:

$('#characterList').find('input[value="' + $(this).val() + '"]').remove();

此外,不是使用$(this).is(":checked")和$(this).val() ,而是使用:

if ( this.checked )

和:

var cur = this.value;

remove() doesn't work like that. You have to provide a selector to select the element you wish to remove. This can be done like so:

$('#characterList').remove('input[value="' + $(this).val() + '"]');

Or, alternatively:

$('#characterList').find('input[value="' + $(this).val() + '"]').remove();

Furthermore, instead of using $(this).is(":checked") and $(this).val(), you can simply use:

if ( this.checked )

And:

var cur = this.value;