【问题标题】:select all checkboxes in Python-CGI Web application with javascript使用 javascript 选择 Python-CGI Web 应用程序中的所有复选框
【发布时间】:2017-09-09 08:55:11
【问题描述】:

我正在开发Python-CGI Web 应用程序。我有一张有 3 列的表,其中第 3 列有复选框。我正在尝试将javascript 用于select all 复选框功能来选择第三列的所有复选框。

这是我的脚本:

#!/usr/bin/python
import cgi, cgitb
cgitb.enable()
print "Content-type:text/html\n"
print "\n\n"
print "<html>"
print "<body>"

bigtempl = '''<html>
<head>
</head>
<body>
    <center>
        <script language="JavaScript">
        function selectAll(source) {
                checkboxes = document.getElementsByName('colors[]');
                for(var i in checkboxes)
                        checkboxes[i].checked = source.checked;
        }
        </script>
        <table border="0" cellspacing="15">
        <tr>
        <th> Number </th>
        <th> Letter </th>
        <th> Select All <input type="checkbox" id="selectall" onClick="selectAll(this)" /> </th>
        </tr>
        {rows}
        </table>
    </center>
    </body>
</html>'''
rowtempl = """
<tr>
    <td> {number} </td>
    <td> {letter} </td>
    <td> <input type="checkbox" name="colors[]" value={check} /> </td>
</tr>
"""

numbers = [0, 1, 2, 3]
letters = ["A", "B", "C", "D"]
checks = [11, 22, 33, 44]

lst = zip(numbers, letters, checks)

rows = [rowtempl.format(number=number, letter=letter, check=check) for number, letter, check in lst]
rows = "".join(rows)

wholepage = bigtempl.format(rows=rows)

print wholepage
print "</body>"
print "</html>"

参考来自here

这是没有代码&lt;script&gt;...&lt;/script&gt;的脚本输出

但它会与 {}&lt;script&gt; 标签混淆。 我收到此错误:

A problem occurred in a Python script. Here is the sequence of function calls leading up to the error, in the order they occurred.
 /root/cgi-bin/prblm.py in ()
     50 rows = "".join(rows)
     51 
=>   52 wholepage = bigtempl.format(rows=rows)
     53 
     54 print wholepage
wholepage undefined, bigtempl = '<html>\n<head>\n</head>\n<body> \n <center>\n ... </table>\n </center>\n </body>\n</html>', bigtempl.format = <built-in method format of str object>, rows = '\n<tr>\n <td> 0 </td>\n <td> A </td>\n <td>...heckbox" name="colors[]" value=44 /> </td>\n</tr>\n'

<type 'exceptions.KeyError'>: '\n\t\tcheckboxes = document'
      args = ('\n\t\tcheckboxes = document',)
      message = '\n\t\tcheckboxes = document' 

有人可以帮我解决这个问题吗?有什么办法可以将javascriptPythonCGI 一起使用?

【问题讨论】:

    标签: javascript python html python-2.7 cgi


    【解决方案1】:

    感谢answer!它解决了这个问题。

    我在函数外添加了{},效果很好!

       <script language="JavaScript">
        function selectAll(source) {{
                checkboxes = document.getElementsByName('colors[]');
                for(var i in checkboxes)
                        checkboxes[i].checked = source.checked;
        }}
        </script>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-02-03
      • 1970-01-01
      • 2015-05-09
      • 1970-01-01
      • 2014-08-29
      • 1970-01-01
      • 2019-10-15
      • 2013-01-24
      相关资源
      最近更新 更多