2017-09-08 3 views
0

Je travaille sur l'application Web Python-CGI. J'ai une table avec 3 colonnes, dans lequel la 3ème colonne a des cases à cocher. J'essaie d'utiliser les cases à cocher javascript pour select all pour sélectionner toutes les cases à cocher de la 3e colonne.sélectionner toutes les cases à cocher dans l'application Web Python-CGI avec javascript

Ceci est mon 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>" 

Référence prise de here.

C'est la sortie du script sans le code <script>...</script>

enter image description here

Mais il s'embrouille avec {} de <script> tag. Je reçois cette erreur:

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' 

que quelqu'un peut me aider à résoudre ce problème? Est-il possible que je peux utiliser javascript avec Python et CGI?

Répondre

0

Grâce à cela, answer! Il a résolu le problème.

J'ai ajouté {} en dehors de la fonction et a bien fonctionné!

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