1

resetおよびsubmit要素を含むフォーム内のすべてのボタンを選択しようとしています。私の現在のセレクターはこれです:

:button, [type=\"submit\"], [type=\"reset\"]

ただし、 では動作しませんreset

これが私のコードです:

HTML:

        <form>

        <input type="text" />
        <input type="text" />
        <input type="text" />
        <select>
            <option>hello</option>
            <option>hi
            </option>
            <option></option>
        </select>
        <textarea></textarea>
        <input type="checkbox" name="hi" value="hi" />
        <input type="radio" name="test" value="hi" />
        <input type="radio" name="test" value="hello" />
        <input type="radio" name="test" />
        <input type="button" />
        <button></button>
        <input type="reset" />
        <input type="submit" />

    </form>

JS:

$(document).ready(function() {

$("form").submit(function() {

    var shouldSubmit = true;

    $(this).children(":input:not(:button, [type=\"submit\"], [type=\"reset\"])").each(function() {

        if ($(this).val() == "")
        {
            shouldSubmit = false;
            return shouldSubmit;
        }

    });

    if ($("input:checkbox").length > 0)
    {
        if ($("input:checkbox:checked").length == 0)
            shouldSubmit = false;
    }

    if ($("input:radio").length > 0)
    {
        if ($("input:radio:checked").length == 0)
            shouldSubmit = false;
    }

    if (shouldSubmit == false)
        alert("All form fields must be filled out.");

    return shouldSubmit;

});

});
4

1 に答える 1

1

このjqueryセレクターを試してください:

$('button, button[type="submit"], button[type="reset"]')
于 2013-07-15T17:28:52.137 に答える