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

发布于 2024-10-19 05:30:08 字数 1977 浏览 2 评论 0原文

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

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

示例设置:

<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>
</div>

然后是脚本:

$('#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());

        });

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

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>
</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());

        });

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

皓月长歌 2024-10-26 05:30:08

您可以这样做:

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

或者这样做,速度较慢,但​​更短且更清晰:

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

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

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

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
}
萌辣 2024-10-26 05:30:08

或者你可以检索 DOM 属性
这里是.prop

选择框的示例代码

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

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

文本框

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

alternatively you can retrieve DOM properties
with .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 }
七秒鱼° 2024-10-26 05:30:08

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

就像我下面使用的例子一样,

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

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

Like in my case I used below,

if($("#id").is("select")) {
  alert('Select'); 
} else if($("#id").is("input")) {
  alert("input");
}
酒浓于脸红 2024-10-26 05:30:08

您可以使用以下代码确定表单元素是输入还是选择:

$('ElementSelector').prop('tagName').toLowerCase();

You can determine if a form element is an input or a select with this code :

$('ElementSelector').prop('tagName').toLowerCase();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文