且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

复选框填充表单字段

更新时间:2023-10-05 23:15:52

you can use Jquery to achive this .. first load the propery jquery library to your project or page ..

then if your checkbox is like this

<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('');
  }
 });
});

check the js fiddle http://jsfiddle.net/2kt0usfx/

if you want to remove the values when uncheck , just

I did not test this , but hope this will work for you