使用jQuery删除不包含任何值的字段 [英] Remove fields that contain no values with jQuery

查看:78
本文介绍了使用jQuery删除不包含任何值的字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

This is a spin-off my earlier question. I have a set of form fields generated via a loop.

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

I also have a link below, that when clicked calls a jQuery function. The link opens a modal dialog which also has a form. I would like to DELETE the fields created via loop that do not contain any values when I submit the form from within the dialog.

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

After testing code I realized that because I submit my form the page actually gets refreshed and I use all values. So, I need to do the opposite -- collect all fields with values and after I submit my form I need to insert all those fields with values back to the page. Now I need help with the following code:

$("#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>

推荐答案

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

这篇关于使用jQuery删除不包含任何值的字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆