将一组用户输入的数据传递到表单的另一个区域

时间:2009-11-17 09:32:19

标签: javascript html mysql

我创建了两个表。第一个是邮寄地址(邮寄街道,城市等)。第二张表是送货地址。这些字段与邮寄地址几乎相同。让我们假设送货地址与邮寄地址相同。如何创建一个单选按钮,将第一个表中的数据复制到第二个表?

用户输入的数据将保存在MySQL数据库中。

有人能告诉我这是怎么做到的吗?这是我到目前为止所尝试的内容:

<script type="text/javascript">
function data_copy(){
    if(document.form1.copy[0].checked){
        document.form1.txtmailing_add2.value=document.form1.txtmailing_add1.value;          document.form1.Address.txtother_street.value=document.form1.Address.txtmailing_street.value;
        document.form1.Address.txtother_city.value=document.form1.Address.txtmailing_city.value;        document.form1.Address.txtother_state.value=document.form1.Address.txtmailing_state.value;
        document.form1.Address.txtother_postcode.value=document.form1.Address.txtmailing_postcode.value;          document.form1.Address.txtother_country.value=document.form1.Address.txtmailing_country.value;
    }
    else{
        document.form1.Address.txtmailing_add2.value="";
        document.form1.Address.txtother_street.value="";
        document.form1.Address.txtother_city.value="";
        document.form1.Address.txtother_state.value="";
        document.form1.Address.txtother_postcode.value="";
        document.form1.Address.txtother_country.value="";
    }
}

</script>

<form name=form1 method=post action="">
</form>

1 个答案:

答案 0 :(得分:0)

<script type="text/javascript">

function data_copy() {

    // assuming naming conventions are consistent.
    var fieldIDs = ['street', 'city', 'state', 'postcode', 'country'];

    var copy = document.getElementsByName('copy'); //using name attribute for reference

    if (copy[0].checked) {

        // note we are expecting the 'id' attribute to be set for the elements.
        var txtmailing_add1 = document.getElementById('txtmailing_add1');
        var txtmailing_add2 = document.getElementById('txtmailing_add2');

        // make sure we have valid objects before accessing their properties
        if (txtmailing_add1 && txtmailing_add2) {
            txtmailing_add2.value = txtmailing_add1.value;
        }

        // for the remaining fields we can look our field id list and generate an id 
        // by combining the expected convention with the given unique string
        for (var j = 0; j < fieldIDs.length; j++) {                
            var mailingField = document.getElementById('txtmailing_' + fieldIDs[j]);
            var otherField = document.getElementById('txtother_' + fieldIDs[j]);
            if (mailingField && otherField) {
                otherField.value = mailingField.value;
            }
        }

    }
    else {
        // same process, just skipping the 'mailing' fields
        var txtmailing_add2 = document.getElementById('txtmailing_add2');

        if (txtmailing_add2) {
            txtmailing_add2.value = '';
        }

        for (var j = 0; j < fieldIDs.length; j++) {
            var otherField = document.getElementById('txtother_' + fieldIDs[j]);
            if (otherField) {
                otherField.value = '';
            }
        }
    }
}

</script>
<form name="form1" method="post" action="" onsubmit="data_copy();">
copy: <br />
<input type="radio" name="copy" value="1" /> yes<br />
<input type="radio" name="copy" value="0" /> no<br />
<!-- form fields -->
</form>