0

我有一个很大的难题,我正在从 MySQL 生成数据到一个表单中,所以问题是,虽然 SQL 生成的第一行是 0,但表单中的选择选项将第一个值解释为 1。不会有任何问题如果我不想从这个查询中抛出一些数据。但这里是代码:

<select id="exp_address" name="exp_address" onChange="document.getElementById('exp_city').value = this.options[this.selectedIndex].getAttribute('data-exp_city'); document.getElementById('exp_dist').value = this.options[this.selectedIndex].getAttribute('data-exp_distr'); document.getElementById('exp_address_val').value = this.options[this.selectedIndex].getAttribute('data-exp_city_val')">
<?php 
for($i = 0; $i < $expselnum; $i++){
    $exps_add_id = mysql_result($expsel,$i,'address_id');
    $exps_address = mysql_result($expsel,$i, "address");
    $exps_city = mysql_result($expsel,$i, "city");
    $exps_distr_val = mysql_result($expsel,$i, "district");
    echo "<option value=$exps_add_id data-num=$i data-exp_city = '$exps_city' data-exp_distr = '$exps_distr_val' data_exp_city_val = '$exps_city'>$exps_address</option>";      
}
?>
</select>

这是生成的 HTML 代码:

<select onChange="Here is some JS to change the value of District and City inputs">
<option value=1 data-num=0 data-exp_city = 'BUCURESTI' data-exp_distr = 'Bucuresti' data_exp_city_val = 'BUCURESTI' [selected by default]>Another Address in RO</option><option value=2 data-num=1 data-exp_city = 'OTOPENI-IF' data-exp_distr = 'Ilfov' data_exp_city_val = 'OTOPENI-IF'>Some address in RO</option></select>
<input name="exp_city" type="text" id="exp_city" style="text-align:left;" value="OTOPENI-IF" size="30" readonly="readonly">

如果你比较选项标签中的 value 和 data-num att,你会发现它们有一个单位不同,WHY。选项的第一个值是在页面登陆时选择的,但城市输入显示查询中的下一个值。我该如何解决这个问题?提前谢谢你们。

4

1 回答 1

0

虽然没有说得很微妙,但我同意 Shrapnel 的评论。但是,这可能会解决您的“问题”(不过,我不太明白这个问题):

for($i = 0; $i < $expselnum; $i++){

改成:

for($i = 1; $i < $expselnum; $i++){
于 2011-09-12T08:42:53.340 回答