我的表单
<form action="javascript:alert( 'success!' );">
<div>
<input type="text" name="username">
<input type="password" name="password">
<input type="submit">
</div>
</form>
我的ajax代码
<script>
$( "form" ).submit(function( event ) {
if ( $( "input:first" ).val() === "correct" && $( "input:second" ).val() === "test" ) {
$( "span" ).text( "Validated..." ).show();
return;
}
$( "span" ).text( "Not valid!" ).show().fadeOut( 10000 );
event.preventDefault();
/// set here your ajax code
});
我如何选择密码字段。 2.这个方法容易被破解吗?
最佳答案
$('input[type="password"]')
或
$('input[name="password"]')
工作演示
$(document).ready(function() {
alert($('input[type="password"]').val())
alert($('input[name="password"]').val())
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div>
<input type="text" name="username">
<input type="password" name="password" value="test">
<input type="submit">
</div>
关于javascript - 我如何在这个ajax代码中选择第二个输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36327636/