简体   繁体   中英

Populate form with data via jquery

I have a simple form that returns make and model data from an API call when a car registration number is entered, I was given javascript that shows this data as a simple alert but I need it instead to fill out another form on the page the code is;

<form name="enterRegForm" method="post" id="enterRegForm">
    <input type="text" id="inputReg" name="inputReg" maxlength= "12" placeholder="ENTER REG" style="width:122px;height:20px;margin:4px 0 0 19px;font:bold 18px tahoma, arial;border:0;text-align:center;"/>
    <input name="button" type="submit" value="Get Report" />
</form>
var inputReg = $('#inputReg');
$('#enterRegForm').submit(function(){
    if (inputReg.val()) {
        $.getJSON("api-proxy.php", { reg: inputReg.val() }, function( data ) {
            // alert("Make: " + data['make'] + "\nModel: " + data['model']);
            alert(data.inspect());
        });
    } 
    else {
        alert('Please enter a registration number!');
    }
    return false;
})

So instead of the alert which shows Make and Model of the car (ie Make: Volkswagen Model:Golf) I need it to fill out two inputs in the second form named "car-make" and "car-model". What do I change to achieve this?

Thank you.

suppose you have input field with id car-make and car-model:

<input id="car-make" value="" name = "car-make">
<input id="car-model" value="" name = "car-model">

and js:

var inputReg = $('#inputReg');
$('#enterRegForm').submit(function(){
    if (inputReg.val()) {
        $.getJSON("api-proxy.php", { reg: inputReg.val() }, function( data ) {
            $("#car-make").val(data['make']);
            $("#car-model").val(data['model']);
        });
    } 
    else {
        alert('Please enter a registration number!');
    }
    return false;
})

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM