我正在为自动化测试制作测试用例。我想检查我的表单中的密码字段是否有type="password"(对于input元素)......
我怎样才能使用 jquery 做到这一点?
谢谢。
$(selector).is(":password")会给你true
$(selector).attr("type")会给你password
点击查看演示
下一条命令将为您提供 ID 为 的 an 元素的类型inputId:
$("#inputId").attr('type');
然后你可以简单地检查任何你想要的关于它的类型。
您可以只使用 :password 选择器并查看它是否返回任何内容
if($(':password').length) {
Do something if exists
}
你可以检查if和.length
if($("#haspassword input[type='password']").length > 0)
演示:http: //jsfiddle.net/xU3se/
上面的示例使用 . 检查特定表单#haspassword。如果length大于零,那么做一些事情。
你试过:password伪类吗?
$type = $("#idOfYourPassField").attr("type");
if($type==='password')
//its a password