Auto populate field base on what was entered in another field simultaneously

前端 未结 4 1879
遇见更好的自我
遇见更好的自我 2021-02-10 04:21

I was trying to figure out how to auto-populate the input value base on what was entered in another input field using javascript. Here\'s my code:



        
相关标签:
4条回答
  • 2021-02-10 04:25

    I made a general example :

    HTML

    <input type="text" class="first">
    <input type="text" class="second">
    

    javascript

    $(".first").on('keyup',function(){
        $(".second").val($(this).val());
    });
    

    http://jsfiddle.net/fmdwv/1/

    For your purpose :

    $("#address").on('change', function(){
      $("#shipping-address-1").val($(this).val());
    });
    
    0 讨论(0)
  • 2021-02-10 04:26

    Create simple html file with two input text box. The first input text box will take the input from the user about a date format and second input text box will display the current date on this format. After the completion of first step, implement date selection library to select a date and format it according to the user input given in first input text b

    0 讨论(0)
  • 2021-02-10 04:31

    The onchange event should be used in the <input> tags themselves and not in the <form>.

    0 讨论(0)
  • 2021-02-10 04:49

    I made a js fiddle based on what you're asking for.

    HTML

    Home Address:
    <input id="address" name="address" type="text" />
    <br/>Apt or Suite Number:
    <input id="suite" name="suite" type="text" />
    <br/>City/Town:
    <input id="city" name="city" type="text" />
    <br/>State:
    <select id="state" name="state" value="">
        <option value="AL">Alabama</option>
        <option value="AK">Alaska</option>
    </select>
    <br/>Zip:
    <input id="zip" name="zip" type="text" value="" />
    <br/>
    <p>Select Shipping Address Below:</p>
    <input type="checkbox" id="shipping-address-1" name="address1" value="">
    <label for="shipping-address-1" id="shiadd1">(Print auto-populated shipping address here...)</label>
    <input type="checkbox" id="shipping-address-2" name="address2" value="">
    <label for="shipping-address-2">ABC Corp 123 Main Street, My City, State Zip</label>
    

    JavaScript

    $("#address,#suite,#city,#state,#zip").change(function () {
        var addressArray = [$("#address").val(), $("#suite").val(), $("#city").val(), $("#state").val(), $("#zip").val()];
        $("#shiadd1").text(addressArray.join(' '));
    });
    
    0 讨论(0)
提交回复
热议问题