2012-01-02 28 views
2

嗨,大家好我無法讓我的代碼正確顯示html代碼,我有。基本上,我正在檢查變量hundo等於3,然後讓腳本顯示一個表單。我測試了代碼,以便在等於3時顯示警告消息,但是當我使用document.write時,它無法工作。我的猜測是,問題在於有...在javascript中使用html的麻煩?

<script> 
setInterval(function(){ 
if(hundo == '3'){ 
document.open(); 
document.write("<form action="insert.php" method="post"> 
Link URL: <input type="text" name="linkurl" /> 
<input type="submit" /> 
</form>"); 
document.close(); 
} 
}, 1000); 
</script> 

回答

1

你需要交替使用的是引號中的document.write

document.write('<form action="insert.php" method="post"> 
Link URL: <input type="text" name="linkurl" /> 
<input type="submit" /> 
</form>'); 

的問題是,你只使用一種類型的引號,所以JavaScript看到"<form action="比undefined javascript對象:insert.php,沒有任何操作員在它們之間啓動。

+0

你說的做完整意義上的和但它仍然不起作用。 – Twister 2012-01-02 19:22:33

+0

「它仍然沒有工作」 - 意思_what_究竟是什麼?錯誤控制檯說什麼? – Oded 2012-01-02 19:27:46

+0

錯誤控制檯中沒有提供任何形式,當hundo = 3時不顯示。 – Twister 2012-01-02 19:29:42

1

問題是在你的文件撰寫的字符串被關閉本身,請嘗試'

<script> 
setInterval(function(){ 
if(hundo == '3'){ 
document.open(); 
document.write('<form action="insert.php" method="post"> 
    Link URL: <input type="text" name="linkurl" /> 
    <input type="submit" /> 
</form>'); 
document.close(); 
} 
}, 1000); 
</script> 
1
<script> 
setInterval(function(){ 
if(hundo == '3'){ 
document.open(); 
document.write('<form action="insert.php" method="post"> 
Link URL: <input type="text" name="linkurl" /> 
<input type="submit" /> 
</form>'); 
document.close(); 
} 
}, 1000); 
</script> 

這將工作圍繞它.. :)