【问题标题】:remove hidden field based on hidden field value using jquery使用jquery根据隐藏字段值删除隐藏字段
【发布时间】:2014-05-13 21:13:39
【问题描述】:

我想根据隐藏字段值使用 jquery 从表单中删除隐藏字段。

请看下面的html:

<input type="hidden" name="myvalue[]" value="value1"/>
<input type="hidden" name="myvalue[]" value="value2"/>
<input type="hidden" name="myvalue[]" value="value3"/>

我有 jquery 函数,它返回我上面的值示例:

$('#getValue').click(function(){

  .... processing code here.....

  return valueOfHiddenField;
});

现在我想根据getValue返回的值从表单中删除隐藏字段

因此,如果该方法返回我value1,则应删除表单中值为value1 的任何输入隐藏字段。在这种情况下,我上面的 html 的第一个输入字段。

任何帮助或建议都会有很大帮助...在此先感谢

【问题讨论】:

  • 你可以使用$('input:hidden').each(function () { if (this.value == 'value1') {$(this).remove()} }来迭代它们
  • @Pilot 谢谢你,先生就像魅力一样

标签: javascript jquery html


【解决方案1】:
$('input[type="hidden"][value="+YOUR_VALUEVAR+"]').remove();

小提琴:http://jsfiddle.net/9tsgy/

【讨论】:

  • 谢谢你先生..因为我知道我只想删除隐藏的输入..我怎么写才能让它只检查表单中的隐藏输入字段..所以它会很快工作。
  • 改成:$("input[type="hidden"][value='+YOUR_VALUEVAR+']").remove();
【解决方案2】:
$('#getValue').click(function () {
    $(document).find("input[type=hidden]").each(function () {
        if ($(this).value == 'Your Value') {
            $(this).remove();
        }
    });
    return valueOfHiddenField;
});

【讨论】:

  • 解释在哪里?
【解决方案3】:

您可以遍历 input:hidden 元素以检查值和 remove() 基于值,

$("input:hidden").each(function () {
    if (this.value == "value1") { // your condition here
        $(this).remove()
    }
}

【讨论】:

  • @Muhammad 很高兴为您提供帮助..如果它满足您的要求,您始终可以将 ans 标记为正确,以便其他人会发现它很容易
  • 非常感谢飞行员先生给我的重要时间..但我对 Rick Lancee 先生的回答更满意...:)
【解决方案4】:

你也可以这样做:

let hiddenValue = $(this).val();
$('input[type="hidden"][value="'+hiddenValue+'"]').remove();

获取隐藏字段的值,然后动态使用并移除特定的&lt;input type="hidden"&gt;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-01-02
    • 2011-10-21
    • 2018-08-21
    • 1970-01-01
    • 2011-03-12
    • 1970-01-01
    • 2011-08-25
    相关资源
    最近更新 更多