【问题标题】:how to check/uncheck the checkboxes using jquery in python web.py如何在 python web.py 中使用 jquery 选中/取消选中复选框
【发布时间】:2012-11-05 01:31:12
【问题描述】:

我正在使用 web.py 框架开发一个小网页,显示数据库中的所有记录。

下面是我的代码

list_page.html

$def with ( select_query )
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>List Page</title>
</head>
<body>
 <form method="POST" action="/retrieve">
 <table border="1">
   <tr>
    <td>Select</td><td>Column_two</td><td>Column_three</td><td>Column_four</td><td>Column_five</td>
   </tr>
  $for r in select_query:
   <tr>
    <td><p align = "center"><input type="checkbox" id="$r.id" value="" name="$r.id"/></p></td>
    <td>$r.Listing_Name</td><td>$r.Address</td><td>$r.Pincode</td><td>$r.Phone</td>
   </tr>
 </table>
   <br/>
  <p><button id="submit" name="select_unselect">Select All/Unselect All</button></p>
  <p><button id="submit" name="submit">Retrieve</button></p>
</form>

所以从上面的html页面中,来自数据库的结果将以表格的形式出现。

实际上,我正在尝试在 python 中使用 jquery 实现检查/取消选中复选框。

下面是我的 index.py 代码

import web   
render = web.template.render('templates/')
db = web.database(dbn='mysql', db='Browser_Date', user='root', pw='redhat')
urls = ('/',   'Listpage',)
app = web.application(urls, globals())

class Listpage:

    def GET(self):
        select_query = db.select('File_upload')
        return render.list_page(select_query)

    def POST(self):
        i = web.input(groups = {})
        ids =  i.keys()
        ........
        ........
        web.header('Content-Type','text/csv')
        web.header('Content-disposition', 'attachment; filename=csv_file.csv')
        return csv_file.getvalue()

if __name__ == "__main__":
    web.internalerror = web.debugerror
    app.run() 

概念是网页(上面显示的html)将显示来自数据库的记录,并基于通过选中/取消选中并单击retrieve按钮后选择的记录,将生成一个csv文件,其中包含页面上的选定记录

所以现在我尝试使用 jquery 一次检查/取消选中所有复选框,但不知道如何开始以及在上面的 html 代码中编写 jquery 代码的位置,我在 jquery 上搜索过,但它真的很混乱,所以我接近了。

基本上我是网络开发的新手,不知道如何实现/使用 jquery, 谁能告诉我如何在上述代码/html文件中实现检查/取消选中复选框功能?这样我就可以轻松地进一步扩展代码

【问题讨论】:

    标签: jquery python html checkbox web.py


    【解决方案1】:

    Python、web.py、html、javascript 和 jQuery 令人困惑。

    它很强大,但当你第一次尝试理解它时也会很困惑。您必须从混合、多平台应用程序的角度来考虑,因为您实际上是将至少三种不同的技术融合到一种用户体验中。

    web.py 服务器和模板只是将静态内容传送到网络浏览器。这就是网络服务器的作用。模板代码允许您在服务器端的 python 中基于查询字符串参数等做出决策,但最终您真正要做的只是将 code (html+javscript) 传递给另一个解释器 (浏览器)。 PHP、Ruby、ASP……在这方面它们都是一样的——为浏览器构建一个页面。

    一旦请求被传递,浏览器就会接管。如果用户点击了某些东西,它将是处理该点击的 javascript 解释器。 Javascript(或 jquery)可以 a) 将用户重定向到不同的页面,b) 在本地操作或 c) 通过 ajax 提交/接收信息。

    (jQuery 是 javascript...它只是简化纯 javascript 的一些怪癖的绝佳方式。)

    所以,一个典型的工作流程:

    • 用户请求http://localhost/index
    • web.py 通过 render() 传递主页
    • 浏览器绘制主页html,并处理任何onload() javascript (页面现在空闲,等待用户交互)
    • 用户点击某物,javascript 函数评估点击的内容并决定请求更多数据
    • javascript 创建并发出 XmlHttpRequest(或在 jQuery 中... $.ajax() )
    • web.py 处理请求,假设它是到 /home_tab_2
    • javascript 更新页面(使用 element.innerHTML() 或在 jquery 中 $(element).append() 或其他)

    我希望这会有所帮助,因为写下来确实对我的第一个 ajax 应用程序有所帮​​助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-02-11
      • 2013-08-04
      • 1970-01-01
      • 1970-01-01
      • 2013-09-09
      相关资源
      最近更新 更多