如果表单用于更新,则 JQuery Validation 禁用验证

我有一个表单,其中的字段在数据库中需要是唯一的,比如用户名,当创建新用户时,验证通过使用远程方法检查用户名是否存在来工作,这工作正常。挑战是我使用相同的表单进行更新,ajax 请求获取用户详细信息并以允许完成更新的相同表单填充它们,但是通过验证,用户名输入发现错误因此无法更新,我如何禁用仅在更新期间使用远程方法,还是可以获得更好的解决方案?



慕慕森
浏览 69回答 1
1回答

缥缈止盈

创建/更新时维护一个输入字段,如下所示value=''当然,value=""在“创建”时和“更新”操作时的某些值(正在更新的记录的 ID),您必须根据正在执行的操作进行设置。<input type="hidden"&nbsp; name="update_id" value='' id="update_id">现在在验证的时候username: {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; required: true,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; remote&nbsp; : {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; type: "POST",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; async: false,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; url: yourUrlHere,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; data: {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; id: function()&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return $("#update_id").val();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}&nbsp; &nbsp; &nbsp; &nbsp; }在这里我们发送 update_id,无论操作是创建/更新即“”,同时创建和更新时的一些值(正在更新的记录的 id)。然后,您可以在页面上使用用户名检查逻辑,url: yourUrlHere,例如 'id'= '' then check from all records for duplicate username or if&nbsp; 'id'= 'someVal'&nbsp; then check from all records for duplicate username except 'id'= 'someVal'
打开App,查看更多内容
随时随地看视频慕课网APP