【问题标题】:Remove Radio button value删除单选按钮值
【发布时间】:2014-04-02 14:30:57
【问题描述】:

我有一些单选按钮,因此存在一些空值,所以我想通过动态识别来删除它

<input type="radio"  name="Result_radio" id="Result_0"  >Group</input>
<input type="radio"  name="Result_radio" id="Result_1"  >individual</input>
<input type="radio"  name="Result_radio" id="Result_2"  >Null Value</input>

【问题讨论】:

  • 您应该在发布问题之前搜索类似的问题。 stackoverflow.com/questions/18992001/…希望链接能有所帮助。并且您应该删除服务器端的空值,这样您甚至不必首先生成单选按钮。

标签: javascript jquery


【解决方案1】:

输入类型radio 没有结束标记。

使用label 作为值

<input type="radio"  name="Result_radio" id="Result_0" > <label>Group</label>
<input type="radio"  name="Result_radio" id="Result_1" ><label>individual</label>
<input type="radio"  name="Result_radio" id="Result_2" ><label>Null Value</label>

jQuery:

$('input:radio').each(function() {
    if($(this).next('label').text() == 'Null Value'){
        $(this).next('label').remove();
        $(this).remove();        
    }
});

Fiddle

【讨论】:

  • 这里输入在标签jsfiddle.net/hamidlab/eju3d内。所以你需要使用一个.remove,你也可以点击文本来选中/取消选中单选按钮。
【解决方案2】:

请注意,您的 HTML 无效,浏览器会将其解释为:

<input type="radio" name="Result_radio" id="Result_0">Group
<input type="radio" name="Result_radio" id="Result_1">individual
<input type="radio" name="Result_radio" id="Result_2">Null Value

因此,我们必须在每次输入后检查下一个文本节点:

// For each radio button
$('input:radio').each(function() {

    // If trimmed text after the current radio button is 'Null Value',
    if($.trim(this.nextSibling.nodeValue) == 'Null Value') {

        // Remove the text node
        $(this.nextSibling).remove();

        // Remove current radio button
        $(this).remove();
    }
});

http://jsfiddle.net/vJ8xN/

【讨论】:

    猜你喜欢
    • 2019-03-23
    • 2010-10-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-24
    相关资源
    最近更新 更多