1

我有一个现有表,我想防止将相同的数据添加到现有表中。
我的问题:为什么当我点击“创建新用户”按钮时,结果总是提示“未找到”?

这是代码: HTML:

<div id="users-contain" class="ui-widget">
    <h1>Existing Users:</h1>
    <table id="users" class="ui-widget ui-widget-content">
        <thead>
            <tr class="ui-widget-header ">
                <th>Name</th>
                <th>Email</th>
                <th>Password</th>
            </tr>
        </thead>
        <tbody>
            <tr>
                <td>guruh</td>
                <td>guruhkharisma1@yahoo.com</td>
                <td>123456</td>
            </tr>
            <tr>
                <td>edo</td>
                <td>edo@yahoo.com</td>
                <td>123456</td>
            </tr>
        </tbody>
    </table>
</div>
<button id="create-user">Create new user</button>

jQuery

    $(function() {
        var name = $( "#name" ),
            email = $( "#email" ),
            password = $( "#password" ),
            allFields = $( [] ).add( name ).add( email ).add( password ),
            tips = $( ".validateTips" );

        function checkExisting2(o) {
            var arr = [];
            $('#users-contain tr').each(function() {

                if (!this.rowIndex) return;
                var  Id = $(this).find("td").eq(0).html();    

                arr.push(Id);
           });

            if ( $.inArray(o,arr) > -1) {
                return false;
            } else {
                return true;
            }
        }

        $( "#dialog-form" ).dialog({
            autoOpen: false,
            height: 300,
            width: 350,
            modal: true,
            buttons: {
                "Create an account": function() { 
                    var bValid = true;

                    bValid = bValid && checkExisting2(name);

                    if ( bValid ) {
                        alert("not found");
                        $( "#users tbody" ).append( "<tr>" +
                            "<td>" + name.val() + "</td>" + 
                            "<td>" + email.val() + "</td>" + 
                            "<td>" + password.val() + "</td>" +
                        "</tr>" ); 
                        $( this ).dialog( "close" );
                    } else {
                        alert("found");
                    }
                },
                Cancel: function() {
                    $( this ).dialog( "close" );
                }
            },
            close: function() {
                allFields.val( "" ).removeClass( "ui-state-error" );
            }
        });

        $( "#create-user" )
            .button()
            .click(function() {
                $( "#dialog-form" ).dialog( "open" );
            });
    });
4

2 回答 2

1

When you write this line:

bValid = bValid && checkExisting2(name);

There's nothing in the code that indicates that name points to anything useful because

var name = $("#name")

points to an empty object.

Also, when you write this:

if (!this.rowIndex) return;

It'll work but it's considered bad form; try this instead:

if (!this.rowIndex) { 
   return; 
}

That way, there's no confusion possible about the code when someone else reads it. Also, all this is redundant:

var bValid = true;
bValid = bValid && checkExisting2(name);
if ( bValid ) { .... }

is equivalent to:

if (checkExisting2(name)) { ... }
于 2012-10-17T22:07:15.967 回答
1

Why are you building the whole array and then iterating it again to check whether it contains the id?

Just use something like this:

    function checkExisting2(o) {
        var rows = $('#users-contain tr'),
            row = null;
        for (var i=1; i<rows.length; i++) {
            if (rows[i].find('td')[0].html() == o)
                return true;
        }
        return false;
    }

Anyways, I'm unsure why your version doesn't work.. try debugging a bit, by placing the word

debugger

on its own line, just before you call $.inArray. Then run the script: when execution gets there, you'll enter debug mode and you can see for sure what's in arr and o.

Side note

var bValid;
bValid = bValid && checkExisting2(name);

is perfectly equivalent to:

var bValid = checkExisting2(name);

(unless, of course, you cut out some code in between the two lines)

于 2012-10-17T22:07:17.823 回答