解决页面内两个表单之间数据相互影响问题

  • 需求:页面先展示不可编辑状态的表单信息,点击编辑按钮,表单信息变为可编辑状态,编辑的内容时,和不可编辑状态的表单之间相互不影响。

  • 解决办法:使用stringifyparse的方法。

  • 问题本质:对象是通过地址指向来获得引用的,所以单纯的用一个新对象指向源对象就是浅拷贝。所以如果使用简单的赋值语句处理后台传过来的值时,两个表单绑定的还是同一个对象,所以相互会有影响。 JSON.stringify()对象序列化为JSON字符串。JSON.parse()JSON数据解析为js原生值。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个简的用户注册和登录页面的代码,使用了HTML、CSS和JavaScript: ```html <!DOCTYPE html> <html> <head> <title>User Registration and Login</title> <style> .container { display: flex; flex-direction: column; align-items: center; } form { display: flex; flex-direction: column; align-items: center; border: 1px solid #ccc; padding: 10px; width: 300px; margin-top: 20px; } input[type=text], input[type=password] { width: 100%; padding: 10px; margin: 5px 0; border: 1px solid #ccc; border-radius: 4px; box-sizing: border-box; } button { background-color: #4CAF50; color: white; padding: 10px 20px; border: none; border-radius: 4px; cursor: pointer; } button:hover { background-color: #45a049; } .switch { display: block; text-align: center; margin-top: 10px; color: #999; font-size: 12px; cursor: pointer; } .switch:hover { color: #666; } </style> </head> <body> <div class="container"> <h1>User Registration and Login</h1> <form id="register-form"> <h2>Registration</h2> <label for="username">Username</label> <input type="text" id="username" name="username" required> <label for="password">Password</label> <input type="password" id="password" name="password" required> <button type="submit">Register</button> </form> <form id="login-form" style="display:none"> <h2>Login</h2> <label for="login-username">Username</label> <input type="text" id="login-username" name="username" required> <label for="login-password">Password</label> <input type="password" id="login-password" name="password" required> <button type="submit">Login</button> </form> <div class="switch" onclick="toggleForm()">Switch to Login</div> </div> <script> function toggleForm() { var registerForm = document.getElementById("register-form"); var loginForm = document.getElementById("login-form"); var switchText = document.querySelector(".switch"); if (registerForm.style.display === "none") { registerForm.style.display = "block"; loginForm.style.display = "none"; switchText.textContent = "Switch to Login" } else { registerForm.style.display = "none"; loginForm.style.display = "block"; switchText.textContent = "Switch to Registration" } } document.getElementById("register-form").addEventListener("submit", function(event) { event.preventDefault(); var username = document.getElementById("username").value; var password = document.getElementById("password").value; console.log("Registration successful with username: " + username + " and password: " + password); this.reset(); }); document.getElementById("login-form").addEventListener("submit", function(event) { event.preventDefault(); var username = document.getElementById("login-username").value; var password = document.getElementById("login-password").value; console.log("Login successful with username: " + username + " and password: " + password); this.reset(); }); </script> </body> </html> ``` 这个代码包含了两个,一个用于用户注册,一个用于用户登录。在页面上方,有一个切换按钮可以在注册和登录之间切换。切换按钮使用JavaScript编写了一个事件监听器,根据当前显示的来切换。 在提交时,JavaScript也编写了事件监听器。当用户在注册中提交时,JavaScript会获取用户名和密码,将其打印到控制台,并重置。当用户在登录中提交时,JavaScript也会获取用户名和密码,将其打印到控制台,并重置。 需要注意的是,这个代码仅为演示用途,未进行任何安全性检查和验证。在实际应用中,需要对用户输入进行验证和加密,以确保用户数据的安全性。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值