gpt4 book ai didi

javascript - 带有 AJAX 和 DOM 处理 API 的下拉菜单

转载 作者:行者123 更新时间:2023-12-03 05:41:37 26 4
gpt4 key购买 nike

我正在从 API 获取数据,但它没有出现在我的下拉列表中。

如果我用 ?act=showprovince 回显,结果就在那里。

示例.html

<head>
<link rel="stylesheet" type="text/css" href="css/normalize.css">
<link rel="stylesheet" type="text/css" href="css/skeleton.css">
<script type="text/javascript" src="js/jquery.min.js">
</script>
<script type="text/javascript" src="js/script.js"></script>
<title>Penggunaan API RajaOngkir | IDMore</title>
<body>
<div class="container">
<div class="row">
<br />
<div class="twelve columns">
<h1>Hitung Ongkos Kirim</h1>
</div>
</div>
<div class="row">
<div class="twelve columns">
<h5>Masukan Data</h5>
</div>
</div>
<div class="row">
<div class="two columns">Asal
<br />
<select id="oriprovince">
<option>Provinsi</option>
</select></div>
<div class="two columns">
<br />
<select id="oricity">
<option>Kota</option>
</select>
</div>
<div class="two columns">Tujuan
<br />
<select id="desprovince">
<option>Provinsi</option>
</select></div>
<div class="two columns">
<br />
<select id="descity">
<option>Kota</option>
</select>
</div>
<div class="two columns">Layanan
<br />
<select id="service">
<option>JNE</option>
<option>POS</option>
<option>TIKI</option>
</select></div>
<div class="two columns">
<br />
<button id="btncheck">Cek Harga</button>
</div>
</div>
<div class="row">
<div class="twelve columns">
<h5>Harga</h5>
</div>
<hr />
<table class="twelve columns">
<tr>
<th>Servis</th>
<th>Deskripsi Servis</th>
<th>Lama Kirim (hari)</th>
<th>Total Biaya (Rp)</th>
</tr>
<span id="resultsbox">
<tr>
<td>...</td>
<td>...</td>
<td>...</td>
<td>...</td>
</tr>
<tr>
<td>...</td>
<td>...</td>
<td>...</td>
<td>...</td>
</tr>
</span>
</table>
</div>
</div>
</body>
</link>
</link>
</head>

流程.php

require_once('idmore.php');

$IdmoreRO = new IdmoreRO();
if(isset($_GET['act'])):

switch ($_GET['act']) {

case 'showprovince':
$province = $IdmoreRO->showProvince();
echo $province;

break;
default:
# code...
break;
}
endif;

idmore.php

class IdmoreRO{
public function __construct()
{

}
//menampilkan data provinsi
public function showProvince()
{
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "http://rajaongkir.com/api/starter/province",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "GET",
CURLOPT_HTTPHEADER => array(
"key: 3f01f13ce2b42ba983ad3f3bc4852f84"
),
));
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
$result = 'error';
return 'error';
} else {
return $response;
}
}
}

脚本.js

$(document).ready(function(){
loadProvinsi('#oriprovince');
loadProvinsi('#desprovince');

$('#oriprovince').change(function(){
alert('yussan');
});

$('#desprovince').change(function(){
alert('yussan');
});
});

function loadProvinsi(id){
$('#oricity').hide();
$('#descity').hide();
$(id).html('loading...');
$.ajax({
url:'process.php?act=showprovince',
dataType:'json',
success:function(response){
$(id).html('');
$province = '';
$.each(response['rajaongkir']['results'], function(i,n){
province = '<option value="'n['province_id']'">'+n['province']+'</option>';
province = province + '';
$(id).append(province);
});
},
error:function(){
$(id).html('ERROR');
}
});
}

最佳答案

我检查了您的代码,您的所有问题都在这行JavaScript函数loadProvinsi()中:

province = '<option value="'n['province_id']'">'+n['province']+'</option>';

您错过了两个“+”符号!

所以正确的代码如下:

province = '<option value="'+n['province_id']+'">'+n['province']+'</option>';

代码的其余部分看起来不错,通过这两个小更改,下拉列表将显示省份

如果您可能需要完整的 JavaScript 函数 loadProvinsi() 将如下所示:

function loadProvinsi(id){
$('#oricity').hide();
$('#descity').hide();
$(id).html('loading...');
$.ajax({
url:'process.php?act=showprovince',
dataType:'json',
success:function(response){
$(id).html('');
$province = '';
$.each(response['rajaongkir']['results'], function(i,n){
province = '<option value="'+n['province_id']+'">'+n['province']+'</option>';
province = province + '';
$(id).append(province);
});
},
error:function(){
$(id).html('ERROR');
}
});
}

关于javascript - 带有 AJAX 和 DOM 处理 API 的下拉菜单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40500925/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com