简体   繁体   中英

select all checkboxes in Python-CGI Web application with javascript

I am working on Python-CGI Web application. I have one table with 3 columns, in which 3rd column has checkboxes. I am trying to use javascript for select all checkboxes functionality to select all the checkboxes of 3rd column.

This is my script:

#!/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>"

Reference taken from here .

This is the output of the script without the code <script>...</script>

在此处输入图片说明

But it gets confused with {} of <script> tag. I am getting this error:

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' 

Can someone help me in resolving this issue? Is there any way I can use javascript with Python and CGI ?

Thanks to this answer ! It resolved the issue.

I added {} outside the function and worked fine!

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM