将表单中多个输入框的值复制并连接到一个输入字段

时间:2011-05-06 01:36:00

标签: jquery forms input

复制/复制并将多个输入框的值连接到一个

我有街道,城市,州和邮政的输入字段和第五个说完整地址的字段。我正在尝试将完整地址[与作为分隔符连接]与完整地址上填写的所有上述四个字段连接起来。

这是我到目前为止所拥有的。第一个复制(街道地址),但我需要帮助连接其他字段...

你可以在这里小提琴: http://jsfiddle.net/subigya/PeVAj/

型= “文本/ JavaScript的” >

<script type="text/javascript">

     $('#street').bind('keypress blur', function() {

        $('#latlongaddress').val($(this).val());

    });

    $('#city').bind('keypress blur', function() {

    ..need help ...

    });

</script>



<form method="post" action="#">

    <div class="formitem">
        <label for="name_of_salon">Name of Salon:</label>
        <input type="text" id="name_of_salon" name="name_of_salon" />
    </div>

    <div class="formitem">
        <label for="street">Street Address:</label>
        <input type="text" id="street" name="street" />
    </div>

    <div class="formitem">
        <label for="city"> City:</label>
        <input type="text" id="city" name="city" />
    </div>

    <div class="formitem">
        <label for="state">State:</label>
        <select name="state" size="1">
            <option value="AK">AK</option>
            <option value="AL">AL</option>
            <option value="AR">AR</option>
            <option value="AZ">AZ</option>
            <option value="CA">CA</option>
            <option value="CO">CO</option>
            <option value="CT">CT</option>
            <option value="DC">DC</option>
            <option value="DE">DE</option>
            <option value="FL">FL</option>
            <option value="GA">GA</option>
            <option value="HI">HI</option>
            <option value="IA">IA</option>
            <option value="ID">ID</option>
            <option value="IL">IL</option>
            <option value="IN">IN</option>
            <option value="KS">KS</option>
            <option value="KY">KY</option>
            <option value="LA">LA</option>
            <option value="MA">MA</option>
            <option value="MD">MD</option>
            <option value="ME">ME</option>
            <option value="MI">MI</option>
            <option value="MN">MN</option>
            <option value="MO">MO</option>
            <option value="MS">MS</option>
            <option value="MT">MT</option>
            <option value="NC">NC</option>
            <option value="ND">ND</option>
            <option value="NE">NE</option>
            <option value="NH">NH</option>
            <option value="NJ">NJ</option>
            <option value="NM">NM</option>
            <option value="NV">NV</option>
            <option value="NY">NY</option>
            <option value="OH">OH</option>
            <option value="OK">OK</option>
            <option value="OR">OR</option>
            <option value="PA">PA</option>
            <option value="RI">RI</option>
            <option value="SC">SC</option>
            <option value="SD">SD</option>
            <option value="TN">TN</option>
            <option value="TX">TX</option>
            <option value="UT">UT</option>
            <option value="VA">VA</option>
            <option value="VT">VT</option>
            <option value="WA">WA</option>
            <option value="WI">WI</option>
            <option value="WV">WV</option>
            <option value="WY">WY</option>
        </select>
    </div>

    <div class="formitem">
        <label for="zip">Zip:</label>
        <input type="text" id="zip" name="zip" />
    </div>

    <div class="formitem">
        <label for="address_of_salon">Full Address:</label>
        <input type="text" id="latlongaddress" name="address" />
    </div>


    <div class="formitem">
        <input type="submit" id="location_submit" name="location_submit" value="Add Location" class="addBtn" />
    </div>

2 个答案:

答案 0 :(得分:13)

使用+进行Javascript连接:

$('#latlongaddress').val($('#street').val() + ' ' +
                         $('#city').val() + ', ' +
                         $('#state').val() + ' ' +
                         $('#zip').val() );

请参阅jsFiddle

答案 1 :(得分:13)

只是为了好玩:

$('#latlongaddress').val( $('#street,#city,#state,#zip').map(function(){
    return $(this).val();
}).get().join(' ') );

eykanal的答案更好,因为它会更快,更容易理解。实际上只能将其用于.val($('input')...

之类的内容