我正在尝试在执行MySQL查询时将字符串输出到html。它似乎工作,当它回应它它给我错误。代码是:
$result = mysqli_query($con,'SELECT * FROM vehicle_type ORDER BY cat_id desc');
while($row = mysqli_fetch_array($result)) {
$category = $row['category'];
$count++;
$vehicle_types .= "<a href='#' onclick='document.getElementById('vehicle_types').value='$count';$('#exec').show();$('#coach').hide();$('#minibus').hide();$('#limos').hide();vehicle_type_name.innerHTML = 'Vehicle type selected : Executive vehicle';vehicle_selected.innerHTML = ''; ' class='btn new btn-primary'>$category</a>";
}
当我回应结果时,它给了我:
<a href='#' onclick='document.getElementById('vehicle_types').value='1';$('#exec').show();$('#coach').hide();$('#minibus').hide();$('#limos').hide();vehicle_type_name.innerHTML = 'Vehicle type selected : Executive vehicle';vehicle_selected.innerHTML = ''; ' class='btn new btn-primary'>Minibus</a>
什么时候应该给我:
<a href="#" onclick="document.getElementById('vehicle_type').value='1';$('#exec').show();$('#coach').hide();$('#minibus').hide();$('#limos').hide();vehicle_type_name.innerHTML = 'Vehicle type selected : Executive vehicle';vehicle_selected.innerHTML = ''; " class="btn new btn-primary">Executive cars</a>
由于
答案 0 :(得分:0)
我建议将所有onClick语句移到函数中以提高可读性。
此外,与您使用jQuery一致。
$vehicle_types .= '<a href="#" onclick="vehicleType_onClick(\'' . $count . '\')"
class="btn new btn-primary">' . $category . '</a>';
<script>
function vehicleType_onClick(count){
$('#vehicle_types').val(count);
$('#exec').show();
$('#coach').hide();
$('#minibus').hide();
$('#limos').hide();
$('input[name=vehicle_type_name']).html('Vehicle type selected : Executive vehicle');
$('input[name=vehicle_selected']).html('');
}
</script>