[英]How can I use the same fields for two different forms?
I'm trying to combine the login and register forms on a WooCommerce/WordPress site. 我正在尝试在WooCommerce / WordPress网站上结合登录和注册表单。 The idea is that a single set of fields, username and password, could be submitted by two different forms.
想法是可以通过两种不同的形式提交一组字段(用户名和密码)。 The first way I thought of is (simplified for clarity):
我想到的第一种方法是(为清楚起见而简化了):
<form id="login">
<input id="username">
<input id="password">
<button type="submit">LOG IN</button>
</form>
<form id="register">
<div style="visibility:hidden!important;position:fixed!important;">
<input id="register_username">
<input id="register_password">
</div>
<button type="submit">REGISTER</button>
</form>
Basically, the layout hides the second pair of inputs but shows both buttons. 基本上,布局隐藏了第二对输入,但同时显示了两个按钮。 Then, there's some JS that mirrors the values of corresponding fields:
然后,有一些JS可以反映相应字段的值:
var u = $('#username');
var p = $('#password');
var ru = $('#register_username');
var rp = $('#register_password')
$('#login').on('change blur focus click keyup',function(){
ru.val(u.val());
rp.val(p.val());
});
This seems to trigger a warning that an "invalid field is not focusable" - which I understand - but, can this be solved and done well? 这似乎触发了一个警告,即“无效字段不可聚焦”-据我了解-但是,可以解决此问题并做得很好吗? Is there a way to do this without JavaScript?
没有JavaScript,有没有办法做到这一点? Is there a better way altogether?
总共有更好的方法吗?
Let's assume I will show the hidden stuff in the case that there is no JS on the user's browser. 假设在用户浏览器中没有JS的情况下,我将显示隐藏的内容。 Let's also assume I was given this design and asked to implement it, ie this is not a question about UX .
我们还假设给了我这个设计并要求实现它,即这不是关于UX的问题 。
Just merge 2 forms into one and set 2 buttons 只需将2个表单合并为一个并设置2个按钮
<form id="login">
<input id="username">
<input id="password">
<button name="submit" type="submit" value="login">LOG IN</button>
<button name="submit" type="submit" value="registration">REGISTER</button>
</form>
After submitting your form you need to check submit value like 提交表格后,您需要检查提交值,例如
if($_POST['submit'] == 'login')
then do code for login
else if($_POST['submit'] == 'registration')
then do code for registration
To reveal the hidden fields in the case of no javascript you would put the data between the following tags: 要在没有JavaScript的情况下显示隐藏字段,可以将数据放在以下标记之间:
As far as the fields that are active when there is JavaScript, place that data within the JavaScript itself using document.write("fields here");. 至于使用JavaScript时处于活动状态的字段,请使用document.write(“ fields here”);将数据放入JavaScript本身。
The end result will be that these fields appear when JavaScript is enabled, and do not appear when JavaScript is disabled. 最终结果是,这些字段在启用JavaScript时出现,而在禁用JavaScript时不出现。
Hope this helps. 希望这可以帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.