我正在制作自动化测试的测试用例。我想检查我的表单中的密码字段是否有type="password"
(对于input
元素)或不...检查输入字段“type”属性的值
我该如何使用jQuery来做到这一点?
谢谢。
我正在制作自动化测试的测试用例。我想检查我的表单中的密码字段是否有type="password"
(对于input
元素)或不...检查输入字段“type”属性的值
我该如何使用jQuery来做到这一点?
谢谢。
下一个命令会给你一个元素的类型与inputId
的ID:
$("#inputId").attr('type');
然后,你可以简单地检查你想了解它的类型什么。
$type = $("#idOfYourPassField").attr("type");
if($type==='password')
//its a password
有更好的方法来达到这个目的,肯定使用jQuery – 321X
你可以只使用:密码选择,看看它是否返回任何
if($(':password').length) {
Do something if exists
}
你可以if
和.length
if($("#haspassword input[type='password']").length > 0)
演示检查:http://jsfiddle.net/xU3se/
以上示例使用#haspassword
检查特定表单。如果length
大于零,那么做点什么。
感谢@ Soltris –
你试过:password
伪类吗?
不,但我现在用它..谢谢。 –
$(selector).is(":password")
会给你true
$(selector).attr("type")
会给你password
点此查看demo
感谢@nayish .. –