我在python和html中有一个输入提交按钮它还没有做任何事情,但是当我添加另一个隐藏的表单值来用来存储变量时,提交按钮就会消失。任何人都可以解释原因吗?
按钮在那里有这段代码 -
#! /usr/bin/python
print 'Content-type: text/html'
print ''
import cgi
import random
form = cgi.FieldStorage()
if "guess" in form:
guess = form.getvalue("guess")
else:
guess = ""
if "answer" in form:
answer = form.getvalue("answer")
else:
answer = ""
for i in range(4):
answer += str(random.randint(0,9))
print answer
print '<h1>Mastermind</h1>'
print '<form>'
print '<input type="text" name="guess" value="' + guess + '">'
print '<input type="hidden" name="answer" value="' + answer + '">'
print '<input type="submit" value="Guess!">'
print '</form>'
但如果我添加另一个值为'numberOfGuesses'
的消息,则会消失#! /usr/bin/python
print 'Content-type: text/html'
print ''
import cgi
import random
form = cgi.FieldStorage()
if "guess" in form:
guess = form.getvalue("guess")
else:
guess = ""
if "answer" in form:
answer = form.getvalue("answer")
else:
answer = ""
for i in range(4):
answer += str(random.randint(0,9))
print answer
print '<h1>Mastermind</h1>'
print '<form>'
print '<input type="text" name="guess" value="' + guess + '">'
print '<input type="hidden" name="answer" value="' + answer + '">'
print '<input type="hidden" name="numberOfGuesses" value="' + numberOfGuesses + '">'
print '<input type="submit" value="Guess!">'
print '</form>'
答案 0 :(得分:1)
并不是说您的按钮正在消失,而是您的程序遇到了异常,因为numberOfGuesses
未定义。
要解决此问题,您只需在访问代码之前在代码中的某处定义numberOfGuesses
。
numberOfGuesses = "42"
print '<h1>Mastermind</h1>'
print '<form>'
print '<input type="text" name="guess" value="' + guess + '">'
print '<input type="hidden" name="answer" value="' + answer + '">'
print '<input type="hidden" name="numberOfGuesses" value="' + numberOfGuesses + '">'
print '<input type="submit" value="Guess!">'
print '</form>'