如果您選擇使用jQuery,您可以使用click
事件,然後獲取該值並更改文本框的值。考慮下面這個例子:Sample Fiddle
<?php
$result = array();
// sample select pseudo code since you didn't provided the db fetching
// $con = new msqli('host', 'username', 'password', 'database');
// $query = mysqli_query($con, 'SELECT address_name FROM table_blah_blah_blah_address');
// while($row = $query->fetch_assoc()) {
// $result[] = $row;
// }
// lets save it contains addresses fetched from db (SAMPLE!)
$result = array(
array('name' => 'address 1'),
array('name' => 'address 2'),
array('name' => 'address 3'),
array('name' => 'address 4'),
);
?>
<form name="form1" method="post" action="">
Please enter address:
<input type="text" name="address" id="address" value="Some Address" style="width:350px" />
<input type="reset" value="Revert" />
<input type="submit" name="GO" id="GO" value="GO!">
</form>
<div class="addresses">
<?php foreach($result as $line_num => $address): ?>
<a href="#" class="clickable_address" data-value="<?php echo $address['name']; ?>" style="cursor: pointer;"><?php echo ++$line_num.'. '. $address['name']; ?><br/></a>
<?php endforeach; ?>
</div>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$('.clickable_address').on('click', function(){
$('#address').val($(this).data('value'));
});
});
</script>
_「但是下面還有其他地址。」_真的嗎?我沒有看到任何。 – j08691
@ Fred-ii-我想你正在接近我所接近的地方。但是,如何讓地址可點擊?並用「某些地址」替換它。這些地址1,地址2是通過使用php的db動態生成的。 – bmm