0

我希望使用复选框来填充表单字段。

示例用户流程:
访客填写运输信息字段,如姓名、地址、邮编、城市。
访客可以选择复选框来自动填充账单信息字段。
如果用户选中“与帐单信息相同”复选框,则
带有输入数据的运输信息字段将被复制/粘贴到帐单信息字段中。

请访问:http ://staelements.com/register

动态填充复选框自动表单复制粘贴字段

J$ 是实现这一目标的最佳方式吗?
请告诉我是否有更好的解决方案。
感谢您的关注好公民!

4

1 回答 1

7

您可以使用 Jquery 来实现这一点..首先将属性 jquery 库加载到您的项目或页面..

那么如果你的复选框是这样的

<input type="checkbox" id="checkbox1" />

<!-- account form fields  -->
<input type="text" id="name" />
<input type="text" id="phone" />

<!-- billing form fields-->
<input type="text" id="name_billing" />
<input type="text" id="phone_billing" />

$(document).ready(function(){
 $("#checkbox1").change(function() {
  if(this.checked) {
   alert("checked ");
    //get the values of the filled fields
    $name = $("#name").val();
    $phone = $("#phone").val();
    console.log("name");
    // then add those values to your billing infor window feilds 

    $("#name_billing").val($name);
    $("#phone_billing").val($phone);

    // then form will be automatically filled .. 

  }
  else{
   $('#name_billing').val('');
   $("#phone_billing").val('');
  }
 });
});

检查 js 小提琴http://jsfiddle.net/2kt0usfx/

如果您想在取消选中时删除这些值,只需

我没有对此进行测试,但希望这对您有用

于 2014-08-15T05:03:02.197 回答