如何将HTML形式的文本输入名称设置为可变
How to set name of text input in HTML form to be variable
我正在尝试在我的 HTML 表单中创建文本输入,但希望该输入具有由变量设置的名称值:
#! c:\Python24\python
print "Content-Type: text/html\n"
import cgi,cgitb
cgitb.enable()
name1 = "kuba"
val1 = 150
name2 = "pipi"
val2 = 300
print("<form action='python2.py' method='GET'>")
print("<input type='text' name='name1'>")
print("<input type=submit value='name2'>")
我希望我的文字名称为 "kuba" 而不是 "name1"
有人可以给我提示吗?
谢谢,
雅库布
您可以像这样在您的字符串中使用 .format()
,假设 name1
是您想要在第二个打印方法中使用的变量。那么你会做:
#! c:\Python24\python
print "Content-Type: text/html\n"
import cgi,cgitb
cgitb.enable()
name1 = "kuba"
val1 = 150
name2 = "pipi"
val2 = 300
print("<form action='python2.py' method='GET'>")
print("<input type='text' name='{0}'>".format(name1))
print("<input type=submit value='name2'>")
因此第二个 print()
会输出 <input type='text' name='kuba'>
。
根据 OP 的要求,对于 python 2.4
print("<input type='text' name='%s'>"%name1)
同样,您可以使用其他连接选项,如评论中@Rafalsonn 所指出的 +
。
我正在尝试在我的 HTML 表单中创建文本输入,但希望该输入具有由变量设置的名称值:
#! c:\Python24\python
print "Content-Type: text/html\n"
import cgi,cgitb
cgitb.enable()
name1 = "kuba"
val1 = 150
name2 = "pipi"
val2 = 300
print("<form action='python2.py' method='GET'>")
print("<input type='text' name='name1'>")
print("<input type=submit value='name2'>")
我希望我的文字名称为 "kuba" 而不是 "name1"
有人可以给我提示吗? 谢谢, 雅库布
您可以像这样在您的字符串中使用 .format()
,假设 name1
是您想要在第二个打印方法中使用的变量。那么你会做:
#! c:\Python24\python
print "Content-Type: text/html\n"
import cgi,cgitb
cgitb.enable()
name1 = "kuba"
val1 = 150
name2 = "pipi"
val2 = 300
print("<form action='python2.py' method='GET'>")
print("<input type='text' name='{0}'>".format(name1))
print("<input type=submit value='name2'>")
因此第二个 print()
会输出 <input type='text' name='kuba'>
。
根据 OP 的要求,对于 python 2.4
print("<input type='text' name='%s'>"%name1)
同样,您可以使用其他连接选项,如评论中@Rafalsonn 所指出的 +
。