在选中时复制表单的相似字段

时间:2013-06-03 07:02:30

标签: javascript forms select duplication

我有一份表格,其中有永久地址和通讯地址 我希望用户先输入永久地址,然后给出一个复选框,说明通信地址是否与永久地址相同。我能够用javascript填充文本框但不能在选择即下拉列表中执行此操作。我怎样才能做到这一点?

我的表单代码是:

<form action="" name="form1" >
    <fieldset>
        PERMANENT ADDRESS:<br />
        <br/> 
        HOUSE/DOOR<input type="text" name="hno1" placeholder="HOUSE/DOOR NUM" required><br/>
        STREET<input type="text" name="street1" placeholder="STREET" required><br/>
        CITY<input type="text" name="city1" placeholder="CITY" required><br/>
        DISTRICT<input type="text" name="district1" placeholder="DISTRICT" required>    <br/>
        STATE<select required name="state1">
                <option>ANDHRA PRADESH</option>
                <option>KARNATAKA</option>
            </select><br />
        COUNTRY<select name="country1" required>
                    <option>INDIA</option>
                    <option>USA</option>
                </select><br />
        PIN<input type="text" name="pin1" placeholder="PIN" required><br/><br />
        CORRESPONDENT ADDRESS:<br />
        <label for="adress same">Same as PERMANENT ADDRESS </label>
        <input name="copy" type="checkbox" onclick="data_copy()"> <br />
        <!--<select required>
        <option>PERMANENT & CORRESPONDENT</option>
        <option>RESPECTIVELY</option>
        </select>-->
        <br/> 
        HOUSE/DOOR <input type="text" name="hno11" placeholder="HOUSE/DOOR NUM"     required><br/>
        STREET<input type="text" name="street11" placeholder="STREET" required><br/>
        CITY<input type="text" name="city11" placeholder="CITY" required><br/>
        DISTRICT<input type="text" name="district11" placeholder="DISTRICT" required>    <br/>
        STATE<select name="state11" required>
                <option>ANDHRA PRADESH</option>
                <option>KARNATAKA</option>
            </select><br/>
        COUNTRY<select name="country11" required>
                    <option>INDIA</option>
                    <option>USA</option>
                </select><br/>
        PIN<input type="text" name="pin11" placeholder="PIN" required><br/><br />
    </fieldset>
</form>

在检查复选框时是否有更好的方法在表单中分配此类重复字段,我还希望在取消选中复选框时清除字段。

2 个答案:

答案 0 :(得分:0)

<select required>
<option>PERMANENT & CORRESPONDENT</option>
<option>RESPECTIVELY</option>
</select>

使用 onchange 事件进行下拉,如下所示,

<select required onChange="data_copy(this.value)">
<option value="1">PERMANENT & CORRESPONDENT</option>
<option value="2">RESPECTIVELY</option>
</select>

然后获取所选选项的值并相应地执行操作。

答案 1 :(得分:0)

这是填充值的函数,

<script type="text/javascript">
function data_copy(str){
if(str=='1'){   // if this the case where both the addresses will be same
   document.form1.hno11.value = document.form1.hno1.value;
   document.form1.street11.value = document.form1.street1.value;
   //.....
   //... so on.. add fields this ways

}

}