添加一个具有select大小的输入文本

Add an input text who have the size of a select

本文关键字:select 文本 输入 一个 添加      更新时间:2023-09-26

我的问题可能有一个简单的答案,但我不知道。

我有一个选择框,它会根据可能的值调整大小,但我无法通过$('#idSlect').attr('size')之类的方法获得它的大小,因为它没有大小属性,它只是调整自己。

如何使输入文本的大小与选择框的大小相同?

我想要的是:

$('#idInputText').size($('#idSelect').size()) 

谢谢你的回答。

检查select的宽度并将输入的宽度设置为相同的值怎么样;

var w = $("#idSlect").css("width");
$("#idInputText").css("width", w);

获取元素高度和宽度并将其应用于另一个元素的简单函数:

function MatchSize(OriginalElement, TargetElement) {
    // Grab the original element height and width
    var CurrentWidth = OriginalElement.width();
    var CurrentHeight = OriginalElement.height();
    // Assign the height and width to the target element
    TargetElement.width(CurrentWidth);
    TargetElement.height(CurrentHeight);
}

然后只提供函数调用中的元素:

MatchSize($('select'), $('input'));

示例

用jQuery捕获元素宽度的方法如下:

$('selector').css('width')

您可以使输入大小相同,如下所示:

$('#input').css('width', $('#select').css('width'))

但最好的方法是使用CSS对页面进行更统一的设计

<style>
    #input, #select{
        width: 100px;
    }
</style>