开发者

Making sure an input is of a certain type with jQuery.each()

开发者 https://www.devze.com 2023-02-10 05:38 出处:网络
I\'m trying to loop over all inputs in my form that are either \"text\" or \"file\". Here\'s my form markup:

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:&l开发者_如何学运维t;/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.

0

精彩评论

暂无评论...
验证码 换一张
取 消