2

我在所有表单元素上运行 .each() 循环。我找不到确定输入类型的方法(无论是复选框、文本、选择、文本区域等......)。

有任何想法吗?

 // When submitting
$("input[name='event_form_submit']").click(function() {
    $("form[name='tepg_form_processor'] :input").each(function() {
        console.log($(this).attr("type"));
       // Determine whether this is a select, input etc?
    });
    return false;
});
4

3 回答 3

2
$("form[name='test'] :input").each(function() {
    if(this.tagName == "INPUT") {
        console.log(this.tagName + ": " + this.type);
    }
    else 
        console.log(this.tagName);
});

小提琴:http: //jsfiddle.net/samliew/YSsvp/8/

可用的输入元素类型

于 2013-02-04T10:20:26.543 回答
1

我建议:

$("form[name='test'] :input").each(function() {
    /* gets the type of the element ('checkbox','text','radio', etc
       if there's no 'type' then it retrieves the tagName of the
       element instead 'select','textarea', etc */
    var inputType = this.type || this.tagName.toLowerCase();
    console.log(inputType);
    /* then does uses a switch to do something depending
       on what you want to do */
    switch (inputType) {
        case 'text':
            // do something with text-inputs
            break;
        case 'radio':
            // do something with radio-inputs
            break;
        case 'select':
            // do something with select-elements
            break;
        // ...etc...
        default:
            // do something with other elements, element-types
            break;
    }
});

JS 小提琴演示

于 2013-02-04T10:24:34.747 回答
0

试试这个(jsfiddle):

$("form[name='test'] :input").each(function() {
    alert(this.type);
});
于 2013-02-04T10:35:00.803 回答