所以,我有一個表單中的按鈕。
<form>
<button onclick="alert('<?php echo $value ?>')">Click me!</button>
</form>
$value = "1.png"
當我按下它時,網址會像這樣更改:
Initial:
index.php?id=82
After I click:
index.php?
如您所見,我希望按鈕僅顯示警報,而不是更改 URL
完整代碼:
<form>
<label>Title</label><br>
<input type="text" value="<?php echo $title ?>"><br><br>
<label>Description</label><br>
<textarea rows="5" maxlength="120"><?php echo $desc ?></textarea><br><br>
<div>
<?php for($k = 0; $k < count($images); $k ) { ?>
<div>
<img src="<?php echo $images[$k] ?>">
<button onclick="alert('<?php echo $images[$k] ?>')">Click me!</button>
</div>
<?php } ?>
</div>
</form>
PS:我不確定是什么問題,但我認為是形式
uj5u.com熱心網友回復:
至少有兩種方法可以實作這一點:
html:
<button type="button" onclick="alert('<?php echo $images[$k] ?>');">Click me</button>
<button onclick="alert('<?php echo $images[$k] ?>');return false;">Click me!</button>
如果您想要實作的唯一目標是提醒文本,我認為第一個選項是最好的。
如果在單擊按鈕時呼叫函式并想要不同的回應,則第二個選項可能會更好:
<button onclick="return foo('<?php echo $images[$k] ?>');">Click me!</button>
在 JavaScript 中:
function foo(image) {
//If image is img1 then update the page
//If any other image, don't update the page
if (image == 'img1') {
return true;
}
return false;
}
uj5u.com熱心網友回復:
當你沒有指定<button>元素的型別時,瀏覽器會暗示它是一個submit型別按鈕。因此,當您按下按鈕時,您的瀏覽器正在提交帶有它的表單。
你也應該,
a) 為按鈕指定除 之外的型別submit,或
b) 在運行的javascript代碼中回傳false,防止點擊事件冒泡。
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/348189.html
上一篇:ob_get_status()在'flags'條目中設定了未記錄的位
下一篇:如何在@media查詢中禁用
