[英]onClick function inside a form with submit button causes error
當我在frontend.php的form標記內使用具有onClick函數的JavaScript按鈕時,會導致錯誤。 我需要一個簡單的應用程序來完成簡單的工作,但是它被卡在了這里。
我為您提供兩個鏈接。 首先是在表單標簽(frontend.php)中,並且那里不起作用-簡單地添加按鈕不會添加更多文本區域! 第二個鏈接沒有form標記,並且它起作用了,您可以嘗試提交,將您帶到welldone.php頁面。
HTML格式
<form action="http://www.balkanex.info/dev/frontend.php" method="post">
Title: <br/><input type="text" name="title"><br/><br/>
The question is: <br/><input type="text" name="ask"><br/><br/>
<br/>
<input type="submit" name="submit" value="PROCEED"><br/>
</form>
前端.PHP文件
<script>
am = 1;
function more(index) {
am++;
var textarea = document.createElement("textarea");
textarea.name = "answer" + am;
var div = document.createElement("div");
div.innerHTML = textarea.outerHTML;
document.getElementById("inner1").appendChild(div);
}
</script>
<?php
echo '<form action="welldone.php" method="post">';
$content = "";
$title = $_POST['title'];
$question = $_POST['ask'];
if($_POST['ask'] != "") {
$answer = '<textarea name="answer1"></textarea>';
$more = '<button type="button" name="more" onClick="more();">Add more</button>';
$content .= '1) '.$question.'<br/>'.$answer.'<br/><div id="inner1"></div>'.$more.'<br/><br/>';
}
echo $content;
echo'<br/><input type="submit" value="CALCULATE"></form>';
?>
結果WELLDONE.PHP文件
<?php
echo 'WOW, WELL DONE';
?>
問題在於,當您使用more
,瀏覽器將使用<button name="more">
而不是function more
。 然后,你得到
TypeError: more is not a function
此行為僅在表單中存在,這就是為什么沒有表單時代碼可以工作的原因。
您可以執行以下任一操作來修復它:
<script>
元素而不是內聯onclick
屬性添加事件處理程序。 無論如何,您的代碼完全無效並且處於怪癖模式。 您應該驗證它並糾正錯誤。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.