使用jQuery删除不包含值的字段

时间:2011-05-26 19:32:33

标签: jquery

这是我之前提出的问题。我有一组通过循环生成的表单字段。

我下面还有一个链接,点击时调用jQuery函数。该链接打开一个模式对话框,该对话框也有一个表单。当我从对话框中提交表单时,我想删除通过循环创建的字段,这些字段不包含任何值。


编辑:

在测试代码后,我意识到因为我提交了表单,页面实际上得到了刷新,并且我使用了所有值。所以,我需要做相反的事情 - 用值收集所有字段,在我提交表单后,我需要将所有带有值的字段插回页面。现在我需要以下代码的帮助:

$("#myLink").click(function(){        

   $("#newForm").dialog({
        buttons: {
            "Submit": function() {
                $("myForm").submit();

                // BELOW is where I need help
                var fieldWithValue = "";

                if($('input[value != ""]').length > 0) {
                    $this = $(this);
                    fieldWithValue += $this.parent();
                }
                $("#vals").html(fieldWithValue);                    
            },
            "Cancel": function() {
                $(this).dialog("close");
            }
        }
   });
});

foreach ($arrays as $listValue) {
    echo '
    <div>
       <input type="text" maxlength="100" name="field[]">
    </div>';
}

<div id="vals"></div>
<a href="#" id="myLink">link</a>

2 个答案:

答案 0 :(得分:2)

if ( $.trim( $( this ).val() ) == '' ) { $( this ).parent().remove() }

答案 1 :(得分:1)

$(‘input[value!=""]‘).val().length == 0 

$('input:text[value=""]').parent().remove();