gpt4 book ai didi

javascript - 如何使用jquery防止清除表单中的表单字段无效?

转载 作者:行者123 更新时间:2023-11-28 04:44:52 25 4
gpt4 key购买 nike

当表单提交时,如何防止清除表单字段,例如userid(如果它已经存在)。

我的代码

<form method="POST" role="form" class="form-horizontal">
{% csrf_token %}
<div class="form-group">
<label class="col-sm-2 lab" for="id_username">Username *</label>
<div class="col-sm-10">
<input name="username" id='id_username' class='form-control' placeholder='Username' minlength="5" maxlength="15" type='text' required></div>
</div>

<div class="form-group">
<label class="col-sm-2 lab" for="id_email">E-Mail *</label>
<div class="col-sm-10" >
<input class='form-control' name="email" id='id_email' placeholder='user@mydomain.com' pattern="(^[a-zA-Z0-9_.+-]+@[a-zA-Z]+\.com$)|(^[a-zA-Z0-9_.+-]+@[a-zA-Z]+\.org$)|(^[a-zA-Z0-9_.+-]+@[a-zA-Z]+\.co\.in$)" maxlength="200" type='email' required>
</div>
</div>

<div class="form-group">
<label class="col-sm-2 lab">Password *</label>
<div class="col-sm-10">
<input class="form-control" name="password1" id="id_password1" placeholder="Password" minlength="8" maxlength="15" type="password" required>
</div>
</div>
<div class="form-group">
<label class="col-sm-2 lab2">Password Confirmation *</label>
<div class="col-sm-10">
<input class="form-control" name="password2" id="id_password2" placeholder="Confirm Password" minlength="8" maxlength="15" type="password" required>
</div>
</div>
<input type="submit"/>
</form>

这是我的代码,支持 django

最佳答案

如果表单无效,请尝试此操作,然后它会调用 preventDefault()

$(document).ready(function() {
$('form').on('submit', function(e){
// validation code here
if(!valid) {
e.preventDefault();
}
});
});

关于javascript - 如何使用jquery防止清除表单中的表单字段无效?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43470565/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com