如何使用jquery比较或检查html元素的输入字段类型
How we can compere or check input filed type of html element with jquery
嗨,我正在研究jquery,我正在尝试检查div的子字段类型的警报。我的意思是检查输入字段和类型。请看我的小代码:
if($('#'+Gid).find("input[type=radio]"))
alert("radio");
if($('#'+Gid).find("input[type=checkbox]"))
alert("radio");
if($('#'+Gid).find("select"))
alert("select");
如果div中有任何字段可以发出警告。现在这是在所有条件下进行的。谁来帮帮我吧。由于
请检查
var Gid = "container";
if($('#'+Gid + " > input[type=radio]").length > 0)
console.log("Radio");
if($('#'+Gid + " > input[type=checkbox]").length > 0)
console.log("Checkbox");
if($('#'+Gid + " > select").length > 0)
console.log("Select");
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="container">
<input type="radio" />
<input type="checkbox" />
<select></select>
</div>
您可以通过以下命令检查字段类型
<script>
function findInputType(formObject) {
for (i=0; i<formObject.childNodes.length; i++) {
if (formObject.childNodes[i].tagName == "INPUT") {
if (formObject.childNodes[i].type == "checkbox") {
alert("This is CheckBox type.")
}
if (formObject.childNodes[i].type == "radio") {
alert("This is Radio type")
}
}
if (formObject.childNodes[i].tagName == "SELECT") {
alert("This is Select field")
}
}
</script>
<script>
// method to find the input type
findInputType(document.myform)
</script>
如果您正在查找所有类型的输入元素,包括div中的select,您可以这样做:
$(function() {
var elems = new Set(); //for collecting unique elements
$("#myDiv input").each(function() {
elems.add($(this).attr("type"));
});
if ($("#myDiv select").length > 0) elems.add("select");
for (let item of elems) console.log(item);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div id="myDiv">
<input type="radio" name="rad" />Radio
<br/>
<input type="checkbox" name="rad" />Checkbox
<br/>
<input type="text" />
<br/>
<select>
<option>Select</option>
</select>
</div>
或者将当前代码更改为如下内容:
$(function() {
var Gid = "myDiv";
if ($('#' + Gid + " input[type=radio]").length != 0)
alert("radio");
if ($('#' + Gid + " input[type=checkbox]").length != 0)
alert("checkbox");
if ($('#' + Gid + " select").length != 0)
alert("select");
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div id="myDiv">
<input type="radio" name="rad" />Radio
<br/>
<input type="checkbox" name="rad" />Checkbox
<br/>
<select>
<option>Select</option>
</select>
</div>
一种应该涵盖大多数用例的方法如下:
var Gid = "container";
// here we look inside the '#container' element to find all
// elements matching the jQuery ':input' selector expression,
// which includes <select>, <input>, <textarea>,
// and then we iterate over each of the found elements (if
// any):
$('#' + Gid).find(':input').each(function(){
// caching the current element for repeated use:
var el = this,
// localName is the same as tagName, but in lowerCase,
// for example 'INPUT' is 'input':
tag = el.localName,
// here we see if the current element has a 'type' property,
// which it does if the tag is equal to 'input'; if it does
// have a 'type' property we assign a string to tell the
// user what that 'type' is; otherwise we return an empty
// string instead (so that the later concatenation will
// still work):
hasType = tag === 'input' ? ', of type="' + el.type + '"' : '',
// a naive check to see if we should say 'an input...' or
// 'a select...':
determiner = ['a','e','i','o','u'].indexOf(tag.charAt(0)) > -1 ? 'an' : 'a';
console.log('This is ' + determiner + ': ' + tag + ' element' + hasType);
});
var Gid = "container";
$('#' + Gid).find(':input').each(function() {
var el = this,
tag = el.localName,
hasType = tag === 'input' ? ', of type="' + el.type + '"' : '',
determiner = ['a', 'e', 'i', 'o', 'u'].indexOf(tag.charAt(0)) > -1 ? 'an' : 'a';
console.log('This is ' + determiner + ': ' + tag + ' element' + hasType);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="container">
<input type="radio" />
<input type="checkbox" />
<select></select>
</div>
JS Fiddle demo.
引用:
- JavaScript:
-
Array.prototype.indexOf()
. - 条件(三元)运算符。
-
Element.localName
. -
String.prototype.charAt()
.
-
- jQuery:
-
each()
. -
:input
选择器
-
相关文章:
- Javascript获取具有不同id的文件数's来自没有多个属性的文件输入元素
- 访问代码生成的输入元素上的keyup事件
- 如何通过jQuery发送选定的元素和文本输入
- 使用 jQuery,当我在输入框外部单击时,如何更改输入元素的值
- 从IE中的不同元素调用时,文件输入控件未正确启动
- 使用jQuery切换来显示内容,但防止在用户单击输入元素时关闭
- 单击其他元素或鼠标向上时隐藏输入框,但保留一次焦点
- 如何在dom元素中插入输入标记数据插件
- 根据输入文本按元素排序,AngularJS
- 更新最大滑块元素输入类型=文本
- 角度/动画引导元素(输入组)
- 如何创建表单元素输入框,标签使用javascript onclick
- 绑定到单击事件以查找子元素输入值
- 特定值元素输入未被读取
- 在JavaScript中将用户输入的元素输入到数组中
- JQuery隐藏元素输入问题遍历
- 如何找出表单是否没有任何元素(输入,选择)在jquery
- 使用表单元素(输入)初始化放大弹出菜单
- 如何获取所有子元素输入
- 基于子元素输入控制父元素:focus