2017-09-08 9 views
0

私はPython-CGI Webアプリケーションを開発中です。私は第3列にチェックボックスがある3列のテーブルを1つ持っています。私は select allチェックボックス機能を使用して、3列目のすべてのチェックボックスを選択しようとしています。Python-CGI Webアプリケーションのすべてのチェックボックスをjavacriptで選択します。

これは私のスクリプトです:hereから取ら

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

リファレンス。

これは、コード<script>...</script>

enter image description here

なしにスクリプトの出力である。しかし、それは<script>タグの{}と混同されます。私はこのエラーを取得しています :

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を使用する方法はありますか?

答えて

0

ありがとうございましたanswer!それが問題を解決しました。

私は機能の外に{}を追加して正常に動作しました!

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