如何让选择框在从python脚本检查时显示其所有内容。

2024-10-08 19:29:26 发布

您现在位置:Python中文网/ 问答频道 /正文

我想要两个选择框,一个是空的,一个是填充的。用户将把一些项目从填充的选择框移到空的选择框中。我有工作。我需要的帮助是,当项目移到空的选择框并按下submit时,不会发送与现在填充的空选择框有关的数据。我发现,这是因为当人移动项目,他们没有保持选中。除了一个选择框之外,还有什么东西看起来像是我可以使用的选择框吗?或者有没有其他方法让我找出从一个框移到另一个框的内容? 我正在用python处理表单,并附上了我正在使用的基本代码。你知道吗

.html格式:

<html>
<head>
</head>

<script language="Javascript">
function SelectMoveRows(SS1,SS2)
{
    var SelID='';
    var SelText='';
    // Move rows from SS1 to SS2 from bottom to top
    for (i=SS1.options.length - 1; i>=0; i--)
    {
        if (SS1.options[i].selected == true)
        {
            SelID=SS1.options[i].value;
            SelText=SS1.options[i].text;
            var newRow = new Option(SelText,SelID);
            SS2.options[SS2.length]=newRow;
            SS1.options[i]=null;
        }
    }
    SelectSort(SS2);
}
function SelectSort(SelList)
{
    var ID='';
    var Text='';
    for (x=0; x < SelList.length - 1; x++)
    {
        for (y=x + 1; y < SelList.length; y++)
        {
            if (SelList[x].text > SelList[y].text)
            {
                // Swap rows
                ID=SelList[x].value;
                Text=SelList[x].text;
                SelList[x].value=SelList[y].value;
                SelList[x].text=SelList[y].text;
                SelList[y].value=ID;
                SelList[y].text=Text;
            }
        }
    }
}
</script>


<body>
<form name="projectBuild" action="../cgi-bin/pythonTest.cgi" enctype="multipart/form-data" method="POST">

<table>
        <tr>
                <td align="center">
                    <SELECT NAME="selectList" style="width: 100%" MULTIPLE>
                    <!--<SELECT NAME="selectList" MULTIPLE>-->
                        <OPTION VALUE="crc">CRC</option>
                        <OPTION VALUE="vector">Vector</option>
                        <OPTION VALUE="mm">Matrix Multiply</option>
                        <OPTION VALUE="bubblesort">Bubble Sort</option>
                        <OPTION VALUE="quicksort">Quick Sort</option>
                    </SELECT>

                </td>
                <td align="center" >

                        <input type="button" name="add" value="Add       >>" onClick="SelectMoveRows(document.projectBuild.selectList,document.projectBuild.userSelections)">
                            <br>
                        <input type="button" name="remove" value="<< Remove" onClick="SelectMoveRows(document.projectBuild.userSelections,document.projectBuild.selectList)">

                </td>
                <td allign="center">
                    <SELECT NAME="userSelections" style="width: 100%" MULTIPLE>
                    <!--<SELECT NAME="userSelections" MULTIPLE>-->
                        <!-- <OPTION VALUE="placeholder">placeholder</option> -->
                    </SELECT>
                </td>

            </tr>

            <tr>
                <td>
                </td>
                <td align="center"> 
                    <input type="submit" value="Submit">
                </td>

</table>
</body>
</html>

.cgi:

#!/usr/bin/python
import cgi, os, sys, commands

myForm = cgi.FieldStorage()
pickedAcc = myForm.getvalue("userSelections")
leftAcc = myForm.getvalue("selectList")
print pickedAcc

print "Left Accelorator list: "
print leftAcc

Tags: textvaluevarselecttdoptionsoptioncgi
1条回答
网友
1楼 · 发布于 2024-10-08 19:29:26

在提交表单之前,遍历userSelections中的所有选项,选择并将每个选项的selected属性设置为true。修改你的表格如下

<form name="projectBuild"
  onSubmit="selectAll(document.projectBuild.userSelections)"
  action="../cgi-bin/pythonTest.cgi"
  enctype="multipart/form-data"
  method="POST">

selectAll方法中,循环所有选项并将selected属性设置为true。你知道吗

Execute a JavaScript when a form is submitted
Javascript loop through ALL HTML select (See the 2nd answer)

相关问题 更多 >

    热门问题