简体   繁体   English

如何将相同的字段用于两种不同的形式?

[英]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.

相关问题 如何在两个不同的脚本中使用相同的 JS function? - How can I use the same JS function in two different scripts? 如何使用Jquery或Javascript同时保存两个不同的表单? - How can i Save two different forms At the same time Using Jquery or Javascript? 如何使用 cy.intercept 存根两个同名请求但返回两个不同的主体? - How can I use cy.intercept to stub two requests of the same name but return two different bodies? 如何将 SmartyStreets 验证应用于同一页面中的两个表单? - How can I apply SmartyStreets validation to two forms in the same page? 如何为同一页面上的两个不同帖子创建两个不同的搜索表单? - How do i create two different search forms for two different posts on a same page? 如何在JavaScript中指向两个字段以使用相同的值 - How do i point two fields to use the same value in javascript 如何检查两个javascript对象是否具有相同的字段? - How can I check if two javascript objects have the same fields? React JS:如何比较两个不同字段的值 - React JS : How can I compare values of two different fields 如何根据条件在两个不同的字段中 $push 一个项目? - How can I $push an item in two different fields, depending on the condition? 如何在同一HTML页面上的三种不同形式下验证同一字段 - How can I validate the same field under three different forms on the same html page
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM