【问题标题】:How do I detect which image button (input type=image) was clicked using webapp2?如何检测使用 webapp2 单击了哪个图像按钮(输入类型 = 图像)?
【发布时间】:2021-04-26 16:18:31
【问题描述】:

如何确定在 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 属性,不使用与其他输入按钮类型相同的方法似乎很奇怪。)

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

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

【问题讨论】:

  • 有人能解释一下需要哪些额外的细节,或者对“确定单击哪个图像按钮的正确方法是什么(在 Python / webapp2 中)的正确方法是什么?”这个问题不清楚吗?以防人们没有阅读全文,我将这句话移到了顶部。
  • 如果您要提出任何解决方法,请检查它是否未在this 等地方提出。 input type=image 无法发送值是众所周知的问题,它并不是真正依赖于后端。无论您使用 Python、ASP、PHP、Go 还是其他任何东西,都是一样的。如果浏览器没有发送任何东西来识别请求中被点击的元素,你就不能在后端做任何事情。

标签: python imagebutton webapp2


【解决方案1】:

我通过在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)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-10-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多