使用jquery基于隐藏字段值删除隐藏字段

时间:2014-04-02 14:03:10

标签: javascript jquery html

我想根据隐藏字段值使用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的第一个输入字段。

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

4 个答案:

答案 0 :(得分:9)

$('input[type="hidden"][value="+YOUR_VALUEVAR+"]').remove();

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

答案 1 :(得分:1)

您可以迭代input:hidden元素以根据值检查值和remove()

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

答案 2 :(得分:0)

$('#getValue').click(function () {
    $(document).find("input[type=hidden]").each(function () {
        if ($(this).value == 'Your Value') {
            $(this).remove();
        }
    });
    return valueOfHiddenField;
});

答案 3 :(得分:0)

你也可以这样做:

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

获取隐藏字段的值,然后动态使用并删除特定的<input type="hidden">

相关问题