将输入从文本框复制到另一个文本框

时间:2017-02-27 11:26:56

标签: javascript jquery html

我正在做一个注册表单,我想添加一个选项,在检查时将文本框中的输入复制到另一个我正在使用一些验证

<td>
    <label for="address" style="text-align: left;">Permanent Address:<sup style="color: red;">*</sup> </label>
</td>
<td>
    <textarea cols="20" id="paddress" name="address1" rows="4"></textarea><i id="pointadrs" style="color: red;"></i>   
</td>
<p>
    <input type="checkbox" name="checkbox" id="checkbox" class="checkbox">use same as my permenant address
</p>
<tr>
    <td><label for="address" style="text-align: left;">Current Address:<sup style="color: red;">*</sup> </label></td>
    <td><textarea cols="20" id="caddress" name="address2" rows="4"></textarea><i id="pointadrs2" style="color: red;"></i> </td>
</tr>

我有验证部分

x = document.getElementById("paddress");
 if (x.value == null || x.value == "") {
    alert("Please enter the permenant Address.");
    x.value = "";
    x.focus();
    return false;
 } else if (x.value.length < 20) {
    alert("Address should be greater than 20 characters.");
    x.value = "";
    x.focus();
    return false;
 }
 x = document.getElementById("caddress");
 if (x.value == null || x.value == "") {
    alert("Please enter the current Address.");
    x.value = "";
    x.focus();
    return false;
 } else if (x.value.length < 20) {
    alert("Address should be greater than 20 characters.");
    x.value = "";
    x.focus();
    return false;
 }

1 个答案:

答案 0 :(得分:-5)

    $(document).ready(function(){
        $('#checkbox').click(function(){
           if($(this).is(':checked')){
               $('textarea#caddress').val($('textarea#paddress').val());
           }else{
               $('textarea#caddress').val('');
           }

        });

    });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<td><label for="address" style="text-align: left;">Permanent Address:<sup style="color: red;">*</sup> </label></td>
  <td><textarea cols="20" id="paddress" name="address1" rows="4"></textarea><i id="pointadrs" style="color: red;"></i>   
  </td>
  <p><input type="checkbox" name="checkbox" id="checkbox" class="checkbox">use same as my permenant address </p>
     <tr>
  <td><label for="address" style="text-align: left;">Current Address:<sup style="color: red;">*</sup> </label></td>
  <td><textarea cols="20" id="caddress" name="address2" rows="4"></textarea><i id="pointadrs2" style="color: red;"></i>   
  </td>