在 jQuery 中确定输入与文本区域

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8300584/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 10:19:22  来源:igfitidea点击:

determining input vs textarea in jQuery

jqueryforms

提问by zeboidlund

I wish to iterate through a form, adding each element to an area. The problem is that some of these elements are select, input, and textboxes. I know that I can use :inputto solve the problem here (though, I don't really need to). The issue is that I'm having trouble determining how I could see whether or not the element is a textarea, input, select, etc. I need to do this properly because, as far as I know, jQuery("input#someinput").val()works great for inputs, but for a textarea I may need jQuery("textarea#sometexarea").text(). I'm not sure if this exists...

我希望遍历一个表单,将每个元素添加到一个区域。问题在于其中一些元素是选择、输入和文本框。我知道我可以用它:input来解决这里的问题(虽然,我真的不需要)。问题是我无法确定如何查看元素是否为 textarea、input、select 等。我需要正确执行此操作,因为据我所知,jQuery("input#someinput").val()对于输入非常有效,但是对于我可能需要的文本区域jQuery("textarea#sometexarea").text()。我不确定这是否存在...

Anyway, here's my function so far:

无论如何,这是我到目前为止的功能:

function getAllFormElements(id) {

    var elements = new Array();

    jQuery(id).children().map(function(){

        for (var index = 0; index < children.length; index++) {

            elements[i] = jQuery(children[i]).val();

        }

    })

        return elements;
}

回答by Frédéric Hamidi

val()works equally well with <input>, <select>and <textarea>elements.

val()<input>,<select><textarea>元素同样有效。

If you still want to determine the actual type of elements matched by the :inputselector, you can use is()with element selectors:

如果您仍然想确定:input选择器匹配的元素的实际类型,可以将is()元素选择器一起使用

$(":input").each(function() {
    var $this = $(this);
    if ($this.is("input")) {
        // <input> element.
    } else if ($this.is("select")) {
        // <select> element.
    } else if ($this.is("textarea")) {
        // <textarea> element.
    }
});

回答by bfink

You should be able to use jQuery's .is() function - just pass a selector to .is() and it will return true if the element matches the selector. So, to see if you had a textarea, you would just check:

您应该能够使用 jQuery 的 .is() 函数 - 只需将选择器传递给 .is() ,如果元素与选择器匹配,它将返回 true 。因此,要查看您是否有 textarea,您只需检查:

if($(this).is("textarea")) { doStuff(); }

http://api.jquery.com/is/

http://api.jquery.com/is/

回答by pomeh

Look at the nodeNameproperty.

nodeName楼盘。

jQuery(id).children().map(function(){
    console.log( children[i].nodeName.toLowerCase() );
});

You can but don't have to use the ismethod from jQuery (overhead here).

您可以但不必使用isjQuery 中的方法(此处为开销)。

回答by lhagemann

Looking at the documentation for jQuery, you can easily get all the form inputs using $(:input): http://api.jquery.com/input-selector/

查看jQuery的文档,您可以使用$(:input)以下方法轻松获取所有表单输入:http: //api.jquery.com/input-selector/

Description: Selects all input, textarea, select and button elements. The :input selector basically selects all form controls.

描述:选择所有输入、文本区域、选择和按钮元素。:input 选择器基本上选择所有表单控件。

.val()also works with text areas, but there is a caveat ... see the docs for reference: http://api.jquery.com/val/

.val()也适用于文本区域,但有一个警告......请参阅文档以供参考:http: //api.jquery.com/val/

Frederic posted his answer as I was writing this, and his solution of using is()certainly proves out.

Frederic 在我写这篇文章时发布了他的回答,他的使用解决方案is()当然得到了证明。

回答by arb

$.val()works for both inputs and text areas. You only need $.text()for things like divs that don't have a "value" per se.

$.val()适用于输入和文本区域。你只需要$.text()像 div 这样本身没有“价值”的东西。

回答by Jose Vega

Are you maybe looking for the $(children[i]).attr("tagName");, which returns the tag of the element, either being select, input, or textarea?

您是否正在寻找$(children[i]).attr("tagName");返回元素标签的 , 是选择、输入还是文本区域?