从HTML下拉框中获取选定的值,而不提交 [英] Getting selected value from drop down box in a html form without submit

查看:421
本文介绍了从HTML下拉框中获取选定的值,而不提交的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何从html表单中的下拉框元素获取所选项目的文本? (使用python)
当我使用鼠标从下拉框中选择一个项目时,如何将值存储到变量中? (即不使用提交按钮)

How to get the text of selected item from a drop down box element in html forms? (using python) How can I store the value to a variable, when I select one item from the drop down box using mouse? (ie. without using a submit button)

这是一个我只在支持Python的应用程序引擎中执行的应用程序。

This is for a application which I am doing in app engine which only supports Python.

推荐答案

您的问题显示了对 Web应用程序的一些误解工作。

Your question shows some misunderstanding on how web applications work.

用户必须在浏览器中键入地址才能访问应用程序。它向服务器发送请求。服务器代码(用python编写)接收到该请求,并有机会发送答案。答案是通常用 HTML 编写的文件。该文档的某些部分可以是动态的,即由python代码生成。文档的其他部分可以是静态的。浏览器然后在用户窗口上呈现文档。

The user has to type an address in a browser to get to the application. That sends a request to the server. The server code (written in python) receives this request and has the opportunity to send an answer. The answer is a document usually written in HTML. Some parts of this document can be dynamic, i.e. generated by the python code. Other parts of the document can be static. The browser then renders the document on the user window.

之后,你的python代码可以知道在浏览器窗口上发生的事情或运行python代码的任何部分的唯一方式是使浏览器发送另一个请求。

After that, the only single way your python code can know about something that happens on the browser window, or to run any part of the python code, is to make the browser send another request.

这可能发生在许多情况下,最常见的是:

That can happen in many situations, the most common being:


  • 用户点击链接到另一个
    网址,使浏览器向这个新网址发送另一个
    请求。

  • 用户点击按钮,使
    浏览器将表单作为请求
    提交到
    表单的操作中配置的地址属性。

  • 实际页面中的一些代码,通常以 ECMAscript (也称为 javascript ),请求引用。

  • User clicks on a link to another url, making browser send another request to this new url.
  • User clicks on submit button, making browser submit the form as a request to the address configured in the form's action attribute.
  • Some code in the actual page, usually written in ECMAscript (also known as javascript), makes a request under the hood.

后者是你要。您必须在 javascript 中编写一些代码,使浏览器将下拉列表中的信息选择发送到服务器。这样你的服务器上的python代码可以做一些事情。

The latter is what you want. You must write in javascript some code to make the browser send the information choosen in the drop down to the server. That way your python code on the server can do something about it.

简单的方法是通过使 onchange 下拉的事件做一个提交:

The simple way to do that is by making the onchange event of the drop down do a submit:

<select name='myfield' onchange='this.form.submit()'>
<option .... >
...
</select>
</form>

这样,当用户更改下拉列表中的值时,表单将被提交,就像用户点击提交。服务器上的Python代码将会运行。浏览器将加载答案。

That way, when the user changes the value on the dropdown, the form will be submited as if the user clicked on submit. Python code on the server will run. The browser will load the answer.

另一种流行的方式是使用javascript的 XmlHTTPRequest DOM API发送请求。这样你就可以在python中收到这个值,然后发送一个答案,这个答案又会被浏览器中的JavaScript代码所接收。该代码可以根据答案更改页面的部分,而无需更改整个页面。这种技术称为 AJAX

Another popular way would be to use javascript's XmlHTTPRequest DOM API to send the request. That way you can receive the value in python and send an answer, which in turn will be received by the javascript code in the browser. That code can change parts of the page based on the answer, without changing the entire page. This technique is called AJAX.

如果您打算编写大量JavaScript代码,我强烈建议您至少使用JavaScript库来缓解处理许多浏览器版本的麻烦。 jQuery 是我的首选图书馆。

If you plan to write lots of javascript code, I strongly suggest using a javascript library at least to ease the pain of dealing with many browser versions. jQuery is my library of choice.

换句话说,在浏览器中运行的JavaScript编写的代码与在服务器中运行的python中编写的代码进行通话。

In other words, code written in javascript running in the browser talks to the code written in python running in the server.

这篇关于从HTML下拉框中获取选定的值,而不提交的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆