简体   繁体   English

通过jQuery用数据填充表单

[英]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; 我有一个简单的表单,可以在输入汽车注册号时从API调用中返回品牌和模型数据,为我提供了将这些数据显示为简单警报的javascript,但是我需要它来在页面上填写代码的另一种形式是;

<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". 因此,除了显示汽车制造商和型号的警报(即制造商:大众汽车模型​​:高尔夫)以外,我需要它在第二个表格中填写两个输入,分别为“ car-make”和“ car-model”。 What do I change to achieve this? 我要做什么才能实现这一目标?

Thank you. 谢谢。

suppose you have input field with id car-make and car-model: 假设您输入的ID为car-make和car-model的字段为:

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

and js: 和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;
})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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