Javascript 在另一个div中显示选项值
在我的数据库中,我有一个表,可以在其中添加宽度、高度以及这个宽度x高度的匹配价格 现在我有了一段代码,在我的网站下拉菜单中显示了宽度x高度。 但我唯一无法做到的是,如果我在下拉列表中单击一个宽度x高度,它的匹配价格会显示在我页面的其他地方Javascript 在另一个div中显示选项值,javascript,php,html,Javascript,Php,Html,在我的数据库中,我有一个表,可以在其中添加宽度、高度以及这个宽度x高度的匹配价格 现在我有了一段代码,在我的网站下拉菜单中显示了宽度x高度。 但我唯一无法做到的是,如果我在下拉列表中单击一个宽度x高度,它的匹配价格会显示在我页面的其他地方 <?php $query = "SELECT * FROM shop_customoptions"; $stmt = $conn->query( $query ); $dropdown = "<sel
<?php
$query = "SELECT * FROM shop_customoptions";
$stmt = $conn->query( $query );
$dropdown = "<select name='id' id='formaat_selecteren'>";
foreach ($stmt as $row) {
$dropdown .= "\r\n<option value='{$row['prijs']}'>{$row['breedte']} x {$row['hoogte']}</option>";
}
$dropdown .= "\r\n</select>";
echo $dropdown;
?>
试试看
<script type="text/javascript">
$("#formaat_selecteren").change(function() {
if ($.isNumeric($(this).val())) {
$(".price").html("€ " + String($(this).val()).replace('.',','));
} else {
$(".price").empty();
};
});
</script>
$(“#format_selecten”).change(函数(){
if($.isNumeric($(this.val())){
$(“.price”).html(“&euro;”+字符串($(this.val()).replace(“.”,“,”);
}否则{
$(“.price”).empty();
};
});
$(文档).ready(函数(){
$(“#format_selecten”)。关于(“更改”,函数(){
if($.isNumeric($(this.val())){
$(“.price”).html(“$”+字符串($(this.val()).replace(“.”,“,”);
}否则{
$(“.price”).empty();
};
});
});
当你说“但它没有用”来显示价格时,我猜你的意思是价格没有格式化,例如“2000欧元”。如果这是真的,你可以试试这样的
PHP-将格式化的价格添加为数据属性。这假设您在PHP中设置了区域设置,并且您可能希望使用不同的格式,请参阅文档:
我宁愿使用.on(“change”,function(){
。确保您有$(文档)。就绪(function(){
…code here})谢谢你的回答,但是它仍然用点而不是逗号来显示价格。哦,你的意思是你想用逗号来替换123.43美分之前的点吗?从123.43到123.43?是的,如果我不清楚的话,很抱歉。我想这样显示价格:€70,60没问题,稍微解释一下,我们取的是元素的值,将其转换为字符串,并用逗号替换第一个句点。感谢您的回答,但它仍然用点而不是逗号显示价格。
<script type="text/javascript">
$("#formaat_selecteren").change(function() {
if ($.isNumeric($(this).val())) {
$(".price").html("€ " + String($(this).val()).replace('.',','));
} else {
$(".price").empty();
};
});
</script>
<script type="text/javascript">
$(document).ready(function() {
$("#formaat_selecteren").on("change", function() {
if ($.isNumeric($(this).val())) {
$(".price").html("$ " + String($(this).val()).replace('.',','));
} else {
$(".price").empty();
};
});
});
</script>
<span class="price"></span>
$dropdown .= "\r\n<option value='{$row['prijs']}' data-price='" . money_format('%.2n', $row['prijs']) . "'>{$row['breedte']} x {$row['hoogte']}</option>";
$("#formaat_selecteren").change(function() {
var formattedPrice = $(this).data('price');
if ($.isNumeric($(this).val())) {
$(".price").html(formattedPrice);
} else {
$(".price").empty();
};
});