سؤال

I'm trying to loop over all inputs in my form that are either "text" or "file".

Here's my form markup:

<form action="" method="post" id="upload_form">
    <p>
        <label for="name">Skin name:</label><br />
        <input type="text" name="name" />
    </p>
    <p>
        <label for="desc">Skin description:</label><br />
        <input type="text" name="desc" />
    </p>
    <p>
        <label for="aname">Authors name:</label><br />
        <input type="text" name="aname" />
    </p>
    <p>
        <label for="aname">Skin image file:</label><br />
        <input type="file" name="skin" />
    </p>
    <p>
        <input type="submit" value="Upload" />
    </p>
</form>

I basically want to check all of these inputs apart from the submit button. Here's my code so far:

$('#upload_form').submit(function(e) {
    $('#upload_form input').each(function(index) {
        if( this.type == "input" ) {

        }
    });
});

But it's wrong, I know. Can anyone tell me how to iterate over each input that is either a text or file input?

Thanks.

هل كانت مفيدة؟

المحلول

You can use the filter or the not selector to accomplish this.

I believe you could do this:

$('#upload_form').submit(function(e) {
    $('#upload_form input').not(':submit').each(function(index) {
        ...
    });
});

I think this would also work:

$('#upload_form').submit(function(e) {
    $('#upload_form input').filter('[type=text], [type=file]').each(function(index) {
        ...
    });
});

نصائح أخرى

Use this.tagName It will give you the tag type as an uppercase string.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top