[英]javascript browser problem works in FF but not in IE and Chrome
<div id = '147'>
<u><b><font color = 'blue' onClick = 'javascript:showbox(147)'>Comment </font</b></u>
</div>
<script type='text/javascript'>
<!--
function showbox(var1) {
document.getElementById(var1).innerHTML = var1 + " <form name = 'commentform' method = 'post' action = ''><input type='text' name = 'comment' value='Enter Your Comment' ><input type='hidden' value= " + var1 + " name='wallpostid'> <input type ='hidden' value = '$userid' name = 'userid' > <input type='submit' name = 'send' value='Post your Comment' onClick='javascript:postcomment()'> </form>";
<b onclick= 'postcomment()' > </b>;
}
function postcomment() {
document.commentform.comment.submit();
}
-->
</script>
以上是代碼。 它在FF中工作,但在IE或Chrome中不起作用。
任何人都可以告訴我哪里出錯了。 任何使其適用於所有瀏覽器的解決方案?
這里有一些拼寫錯誤:
"...<input type='hidden' value= " + var1 + " name='wallpostid'>... "
"...<input type='hidden' value='" + var1 + "' name='wallpostid'/>... "
注意/
和兩個'
也是這一行
<b onclick= 'postcomment()' > </b>;
沒有做任何事情,因為它在javascript而不是文件的html部分。
首先,你的標記有點亂,所以難怪其中有一個混亂。 但是我認為你的問題是$userid
是作為字符串傳遞的,而不是它作為某種變量的值。
這是你的標記整理了一點(但仍然有很多不足之處)刪除了一些冗余的東西, $userid
回應了PHP風格。
<div id="147" onclick="showbox(this)"><a href="#">Comment</a></div>
<script>
function showbox(el) {
el.innerHTML = el.id + '\
<form name"="commentform" method="post" action="">\
<input type="text" name="comment" value="Enter Your Comment" />\
<input type="hidden" value="' + el.id + '" name="wallpostid" />\
<input type="hidden" value="<?php echo $userid; ?>" name="userid" />\
<input type="submit" name="send" value="Post your comment">\
</form>';
}
</script>
document.getElementById(var1).innerHTML = var1 + " <form name = 'commentform' method = 'post' action = ''><input type='text' name = 'comment' value='Enter Your Comment' /><input type='hidden' value= '" + var1 + "' name='wallpostid' /> <input type ='hidden' value = '$userid' name = 'userid' /> <input type='submit' name = 'send' value='Post your Comment' onClick='javascript:postcomment()' /></form><b onclick= 'postcomment()' ></b>";
一堆東西都錯了:
1. ID不能以數字開頭
2.使用onclick
,而不是onClick
3.不要把javascript:
放在你的onclick
你的<b onclick
......沒用
5.你的postcomment
函數只是提交表單 - 你不需要JS
6.確保為表單設置action
這是完整的固定代碼:
<div id='div147'><u><b><font color='blue' onclick='showbox(147)'>Comment</font></b></u></div>
<script type='text/javascript'>
function showbox(var1) {
document.getElementById(var1).innerHTML = var1 + " <form name='commentform' method='post' action='someurl.php'><input type='text' name='comment' value='Enter Your Comment'><input type='hidden' value='" + var1 + "' name='wallpostid'><input type='hidden' value='$userid' name='userid'> <input type='submit' name='send' value='Post your Comment'> </form>';
}
</script>
雖然添加內部html有時會出現一些瀏覽器的運行時錯誤。 提出這個問題,以便更好地理解如何添加內部html 。 另外我不認為你需要onclick功能來提交表格。 它會在點擊提交后自動提交
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.