jQuery:如何提交一个编辑过的用户输入值的表单? [英] jQuery: how to submit a form with edited user inputed values?
问题描述
< h3>
< a href =#>登入< / a>
< / h3>
< div id =tabs-login>
< form method =getaction =./ dev.html>
< fieldset>
<图例>
电子邮件地址:
< / legend>
< input type =textclass =required emailname =login/>
< / fieldset>
< fieldset>
<图例>
密码:
< / legend>
< input type =passwordclass =requiredname =pass/>
< / fieldset>
< input type =submitvalue =Submit/>
< / form>
< / div>
我使用jQuery来验证它:
< script>
$(document).ready(function(){$(form)。validate();});
< / script>
我希望在表单子目录中获取用户输入的密码值并将 SHA256 通过这个jQuery插件并提交 email =用户输入值
和 pass = sha-value
。如何通过jQuery访问验证值并更改它们并发送到原始表单目的地?
这就是说:你会想要在表单提交之前计算SHA值。
您可以将事件处理程序绑定到密码字段的blur事件,以使用新哈希更新隐藏字段的值。或者您可以将更新逻辑添加到验证代码中。
无论额外代码的位置如何,您都可以更轻松地准备好散列 到表单提交比在第一个表单之后发送第二个表单。
例如
<! - 将此添加到表单 - >
< input type =hiddenname =sha_passvalue =/>
//将其添加到文档就绪处理程序
$('[name = pass]')。blur(function(){
$('[name = sha_pass] ').val($ .sha256($(this).val());
});
So for example I have a simple HTML form:
<h3>
<a href="#">Login</a>
</h3>
<div id="tabs-login">
<form method="get" action="./dev.html">
<fieldset>
<legend>
Email:
</legend>
<input type="text" class="required email" name="login" />
</fieldset>
<fieldset>
<legend>
Password:
</legend>
<input type="password" class="required" name="pass" />
</fieldset>
<input type="submit" value="Submit" />
</form>
</div>
And I use jQuery to validte it:
<script>
$(document).ready(function() { $("form").validate(); });
</script>
I want on form submition to take user inputed password value and take SHA256 from it via this jQuery plugin and submit email=user-inputed-value
and pass=sha-value
. How to access validated values via jQuery and change them and send to original form destination?
First -- I want to point out that I don't think this is a good idea. It's generally a bad idea to implement password hashing on the client side. Instead, the pass should be sent in plain text (over HTTPS) to the server, where it is then hashed using your preferred algorithm before storage. This has the added benefit of not advertising to potential attackers which hashing method is used, since that code exists only on the server.
That said: you're going to want to calculate that SHA value prior to form submit.
You could bind an event handler to the password field's blur event to update a hidden field's value with the new hash. Or you could add the update logic to the validation code.
Regardless of where the extra code goes, it will be much easier for you to prepare the hash prior to the form submit than it will be to send a second submission chasing after the first.
E.g.
<!-- add this to the form -->
<input type="hidden" name="sha_pass" value="" />
// add this to the document ready handler
$('[name=pass]').blur(function() {
$('[name=sha_pass]').val($.sha256($(this).val());
});
这篇关于jQuery:如何提交一个编辑过的用户输入值的表单?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!