如何检测使用webapp2单击的图像按钮(输入类型=图像)?

2024-05-05 16:08:23 发布

您现在位置:Python中文网/ 问答频道 /正文

在Google App Engine(GAE)下运行的Python脚本中,如何确定网页上的哪个input type=image(imagebutton)被单击(使用webapp2处理程序)

我有一个web表单,其中有两个(目前可能会更多)图像按钮,我需要知道单击了哪个按钮。我看到了一个关于使用按钮id属性的答案。但是它没有使用Python/webapp2,所以我不确定如何应用它,而且我可能也没有正确理解它

我尝试过很多事情,但都没有成功,这是我的出发点

imgid = handler.request.id

在POST处理程序中,但这会导致属性错误

搜索图像按钮和webapp2请求上的各种信息资源,除了单击指针的图像中的坐标外,我几乎看不到从图像按钮返回服务器的信息。我确实发现value属性不像其他按钮那样与图像按钮一起使用;在另一个环境(ASP.NET,而不是Python/webapp2)中有一篇帖子说要使用id属性,但在Python中不起作用

(奇怪的是,使用value属性时,没有使用与其他输入按钮类型相同的方法。)

这是试图获取id的POST处理程序的代码:

class image_button_set(webapp2.RequestHandler):
    def sails_spin_set(handler, SLurl):
        imgid = handler.request.id

    

这是表单的HTML内容

<form action="/image_button_set" method="post">
    <input name="parm1" id="Button1" type="image" src="/images/spin-glasses3.jpg" height="256" width="256">
    <input name="parm1" id="Button2" type="image" src="/images/spin-spiral3.jpg" height="256" width="256">
    <input name = "parm1" value="Cancel" style="font-size: 16pt; color: DarkRed; font-weight: bold;" type="submit">
</form>

下面是代码如何查找其他类型的按钮,例如收音机(忽略无关的日志代码等):

class image_button_set(webapp2.RequestHandler):
    def sails_spin_set(handler, SLurl):
       image = self.request.get("image")

HTML中的按钮定义是

<input name="image" value="image1" type="radio">Image1
<input name="image" value="image2" type="radio">Image2

Tags: name图像imageid处理程序input属性value
1条回答
网友
1楼 · 发布于 2024-05-05 16:08:23

我通过在imagebutton上使用formaction属性解决了这个问题

<form action="/sails_spin_set?img=dummy" method="post">
  <input type="image" name="spin" formaction="/imgbtn_set?img=spin1" src="/images/spin-glasses3.jpg" style="font-size: 12pt; color: Navy; font-weight: bold;" height="256" width="256">
  <input type="image" name="spin" formaction="/imgbtn_set?img=spin2" src="/images/spin-spiral3.jpg" style="font-size: 12pt; color: Navy; font-weight: bold;" height="256" width="256"><br>
  <br>
  <input name="act" value="Cancel" style="font-size: 16pt; color: DarkRed; font-weight: bold;" type="submit">
</form>

然后以通常的方式处理URL, 主脚本中的路由:

app = webapp2.WSGIApplication(
                              [('/', MainPage),
                               ('/imgbtn_show', image_button_show),
                               ('/imgbtn_set', image_button_set)])
                                  

以及用于此的POST处理程序:

class image_button_set(webapp2.RequestHandler):
    def post(self):
        imageid = self.request.get("img")  # from the selected imagebutton
        handler.response.write("image selected: "+imageid)

相关问题 更多 >