访问ajax/jquery返回的数据并将其放置到表单字段

access ajax/jquery returned data and place it to form field

本文关键字:字段 表单 ajax jquery 返回 数据 访问      更新时间:2023-09-26

用户在此表单上输入一个地址,然后单击验证地址,然后运行如下命令:

$('#verify').click(function () {
    var address = $('input[name=address]');
    var city    = $('input[name=city]');
    var state   = $('input[name=state]');
    var zip     = $('input[name=zip]');
    var country = $('select[name=country]');
    //organize the data for the call
    var data = 'address=' + address.val() + '&city=' + city.val() + '&state=' + state.val() + '&zip='  + zip.val() + '&country='  + country.val();
    //start the ajax
    $.ajax({
        url: "process.php",
        type: "GET",
        data: data,
        cache: false,
        success: function (html) {
            //alert (html);
            if (html!='error') {
                //show the pin long and lat form
                $('.form2').fadeIn('slow');
            } else alert('Error: Your location cannot be found');
        }
    });
    //cancel the submit button default behaviours
    return false;
});

process.php获取数据,并验证地址是否存在,然后将纬度和经度作为"longitude,latitude"返回。如何获取该数据并将其放置在显示位置的表单字段中(form2div中的表单字段)。非常感谢。

替换为

$('.form2').fadeIn('slow');
与这个:

$('.form2').fadeIn('slow').find('input[name=latlon]').val(html);
是要在其中插入经度和纬度的输入字段的名称。

嗯,我有点困惑,但是,如果我理解正确的话,您可以通过","分隔符分割响应,并简单地将每个响应放在各自的字段中。这样的:

if (html!='error') {
    var response = html.split(',');
    $('.form2').fadeIn('slow');
    $('.form2 input.longitude').attr( 'value', response[0] );
    $('.form2 input.latitude').attr( 'value', response[1] );
} else {
    alert('Error: Your location cannot be found');
}