我想使用Brython實現一個功能,我有一個允許我輸入自己名字的表單。輸入我的名字并提交表單后,我希望表單消失,出現一個div,顯示一條歡迎消息& quot歡迎,[姓名]& quot;其中[姓名]是我輸入的姓名。
我已經嘗試了下面的代碼,但是它不能像預期的那樣工作:
from browser import document, html, window
storage = window.localStorage
form = document.getElementById("name-form")
div = document.getElementById("welcome")
if storage["name"]:
form.style.display = "none"
div.style.display = "block"
else:
form.style.display = "block"
div.style.display = "none"
但是,當我運行代碼時,我遇到了以下錯誤:& quotbrython.js:6594未捕獲的目錄...[完整的錯誤消息]& quot;
我已經驗證了我已經在HTML文件中包含了必要的Brython腳本標記。有沒有人能提供如何使用Brython正確實現這一功能的指導?
錯誤消息:
brython.js:6594未捕獲的目錄 at object . eval[as $ factory](at $ b . $ make _ exc的eval(https://cdnjs . cloud flare . com/Ajax/libs/brython/3 . 11 . 2/brython . js:7807:20),5:15) at$b . attr _ error(https://cdnjs . cloud flare . com/Ajax/libs/brython/3 . 11 . 2/brython . js:7834:27) at$B . jsobj . get attribute(https://cdnjs . cloud flare . com/Ajax/libs/brython/3 . 11 . 2/brython . js:9336:10) at $ b . $ getattr(https://cdnjs . cloud flare . com/Ajax/libs/brython/3 . 11 . 2/brython . js:6582:9) at b . dir(https://cdnjs . cloud flare . com/Ajax/libs/brython/3 . 11 . 2/brython . js:6322:35) at offer _ suggestions _ for _ attribute _ error(https://cdnjs . cloud flare . com/Ajax/libs/brython/3 . 11 . 2/brython . js:7898:13) 位于$b . error _ trace(https://cdnjs . cloud flare . com/Ajax/libs/brython/3 . 11 . 2/brython . js:8031:117) at $ b . show _ error(https://cdnjs . cloud flare . com/Ajax/libs/brython/3 . 11 . 2/brython . js:8038:42) at$b . handle _ error(https://cdnjs . cloud flare . com/Ajax/libs/brython/3 . 11 . 2/brython . js:8046:4) at $B.loop (https://cdnjs.clou
如果沒有完整的上下文,很難解釋錯誤消息,但這里有一個最小的示例可以滿足您的要求:
<script type="text/python">
from browser import bind, document, html, window
@bind('#name-form', 'click')
def welcome(ev):
form = document.select_one('form')
name = document["name-input"].value
form.remove()
document["welcome"].text = f'Welcome, {name} !'
</script>
<form method="get" action="#">
<input id="name-input" name="name">
<input id="name-form" type="submit">
</form>
<div id="welcome"></div>