【问题标题】:Passing a set of user entered data to another area of a form将一组用户输入的数据传递到表单的另一个区域
【发布时间】:2009-11-17 09:32:19
【问题描述】:

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

用户输入的数据将保存在 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>

【问题讨论】:

  • 我认为,您可以在单击单选按钮时使用 javascript,将邮寄地址的值传递给送货地址并将值存储在表中
  • 我尝试使用 javascript 来解决它。但没有工作。
  • ok 然后检查 onSubmit 中的条件。如果选中复选框或选项框,则将邮寄地址的值添加到两个表中。
  • 我上传了我的代码 d.可以帮mw看看,觉得我需要改什么?

标签: javascript html mysql


【解决方案1】:
<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>

【讨论】:

    猜你喜欢
    • 2015-12-26
    • 2019-12-16
    • 1970-01-01
    • 2022-08-14
    • 2019-01-14
    • 2018-02-06
    • 2017-03-21
    • 1970-01-01
    • 2019-07-05
    相关资源
    最近更新 更多