jQuery - 确定输入元素是文本框还是选择列表

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5108337/
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-26 18:35:22  来源:igfitidea点击:

jQuery - determine if input element is textbox or select list

jqueryjquery-selectors

提问by ajberry

How would I determine whether the element returned by an :input filter in jQuery is a textbox or select list?

我如何确定 jQuery 中 :input 过滤器返回的元素是文本框还是选择列表?

I want to have a different behavior for each ( textbox returns text value, select returns both key and text)

我想为每个(文本框返回文本值,选择返回键和文本)有不同的行为

Example setup:

示例设置:

<div id="InputBody">
<div class="box">
    <span id="StartDate">
        <input type="text" id="control1">
    </span>
    <span id="Result">
        <input type="text" id="control2">
    </span>
    <span id="SelectList">
        <select>
            <option value="1">Option 1</option>
            <option value="2">Option 2</option>
            <option value="3">Option 3</option>
        </select>
    </span>
</div>
<div class="box">
    <span id="StartDate">
        <input type="text" id="control1">
    </span>
    <span id="Result">
        <input type="text" id="control2">
    </span>
    <span id="SelectList">
        <select>
            <option value="1">Option 1</option>
            <option value="2">Option 2</option>
            <option value="3">Option 3</option>
        </select>
    </span>
</div>

and then the script:

然后是脚本:

$('#InputBody')
    // find all div containers with class = "box"
    .find('.box')
    .each(function () {
        console.log("child: " + this.id);

        // find all spans within the div who have an id attribute set (represents controls we want to capture)
        $(this).find('span[id]')
        .each(function () {
            console.log("span: " + this.id);

            var ctrl = $(this).find(':input:visible:first');

            console.log(this.id + " = " + ctrl.val());
            console.log(this.id + " SelectedText = " + ctrl.find(':selected').text());

        });

回答by user113716

You could do this:

你可以这样做:

if( ctrl[0].nodeName.toLowerCase() === 'input' ) {
    // it was an input
}

or this, which is slower, but shorter and cleaner:

或者这个,它更慢,但更短更干净:

if( ctrl.is('input') ) {
    // it was an input
}


If you want to be more specific, you can test the type:

如果你想更具体,你可以测试类型:

if( ctrl.is('input:text') ) {
    // it was an input
}

回答by Rohit

alternatively you can retrieve DOM properties with .prop

或者,您可以检索 DOM 属性 .prop

here is sample code for select box

这是选择框的示例代码

if( ctrl.prop('type') == 'select-one' ) { // for single select }

if( ctrl.prop('type') == 'select-multiple' ) { // for multi select }

for textbox

用于文本框

  if( ctrl.prop('type') == 'text' ) { // for text box }

回答by Umesh Patil

If you just want to check the type, you can use jQuery's .is() function,

如果你只是想检查类型,你可以使用 jQuery 的 .is() 函数,

Like in my case I used below,

就像我在下面使用的情况一样,

if($("#id").is("select")) {
 alert('Select'); 
else if($("#id").is("input")) {
 alert("input");
}