jQuery - 确定输入元素是文本框还是选择列表
如何确定 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可以这样做:
或者这样做,速度较慢,但更短且更清晰:
如果您想更具体,您可以测试类型:
You could do this:
or this, which is slower, but shorter and cleaner:
If you want to be more specific, you can test the type:
或者你可以检索 DOM 属性
这里是
.prop
选择框的示例代码
文本框
alternatively you can retrieve DOM properties
with
.prop
here is sample code for select box
for textbox
如果你只想检查类型,你可以使用 jQuery 的
.is()
函数,就像我下面使用的例子一样,
If you just want to check the type, you can use jQuery's
.is()
function,Like in my case I used below,
您可以使用以下代码确定表单元素是输入还是选择:
You can determine if a form element is an input or a select with this code :