网页将数据库表motocykle
中的所有记录显示到特殊容器。
看起来像这样:
代码
<div class="dbmaincontainer">
<div class="finddbspecificcontainer">
<div class="finddbimgcontainer">
<?php echo '<img src="data:image/jpeg;base64,'.base64_encode( $row['img'] ).'" />';?>
</div>
<div class="finddbspecificvalues">
<div class="findvalue1">
<label class="blackbold600">MARKA: </label>
<?php echo $row['mark'];?>
</div>
<div class="findvalue1">
<label class="blackbold600">MODEL: </label>
<?php echo $row['model'];?>
</div>
<div class="findvalue1">
<label class="blackbold600">POJEMNOŚĆ: </label>
<?php echo $row['capacity'];?>
<label class="blackbold600"> CC</label>
</div>
<div class="findvalue1">
<label class="blackbold600">MOC: </label>
<?php echo $row['power'];?>
<label class="blackbold600"> KM</label>
</div>
</div>
<div class="finddbbutton">
<button class="button" onclick="alert('<?php echo $row['description'];?>');">Pokaz Opis</button>
</div>
</div>
</div>
问题
单击“Pokaz opis”按钮后,某些警报会显示,有些则不会。问题出在哪里?
最佳答案
如果任何描述包含引号,将其替换到 HTML 中将导致错误,因为这些引号将终止 onclick
属性或 alert()
的字符串参数。您需要对引号进行编码以防止出现这种情况,这可以使用 htmlentities()
来完成。
<button class="button" onclick="alert('<?php echo htmlentities($row['description'], ENT_QUOTES);?>');">Pokaz Opis</button>
关于javascript - 单击按钮后,有些 Javascript 警报会显示,有些则不会。为什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65711668/