Python 根据所选选项显示html下拉列表

Python 根据所选选项显示html下拉列表,python,html,sql,cgi,Python,Html,Sql,Cgi,我正在使用python CGI构建一个查询数据库的小型web应用程序。逻辑是: HTML表单-PythonCGI-SQL查询-PythonCGI打印结果 目前它还可以工作,但现在我被困在创建表单中。我想做的是根据第一张表格的结果自动填写第二张表格。例如: 第一个下拉选项:汽车、摩托车、公共汽车、火车。 第二个下拉列表:如果我选择了汽车,那么只播汽车选项。如果是火车,只有火车选项 我知道如何使用SQL('WHERE type='car')但是我不知道如何填写html表单 我没有使用任何框架,只使用

我正在使用python CGI构建一个查询数据库的小型web应用程序。逻辑是: HTML表单-PythonCGI-SQL查询-PythonCGI打印结果

目前它还可以工作,但现在我被困在创建表单中。我想做的是根据第一张表格的结果自动填写第二张表格。例如:

第一个下拉选项:汽车、摩托车、公共汽车、火车。
第二个下拉列表:如果我选择了汽车,那么只播汽车选项。如果是火车,只有火车选项

我知道如何使用
SQL('WHERE type='car')
但是我不知道如何填写html表单


我没有使用任何框架,只使用HTML、Python(而不是php)和Postgresql。您必须使用ajax:当第一次选择更改时,您必须将所选值传递给远程Python脚本,该脚本处理请求并将结果返回到页面:此时,必须通过javascript填充第二次选择。
数据可以是简单的html或json格式。 这是一个使用jquery和ajax的示例(请记住包括):
html:


汽车
摩托车
公共汽车
javascript:

<script>
$('document').ready(function(){
//  change event handler
$('select.changeStatus').change(function(){
    // You can access the value of your select field using the .val() method
    //alert('Select field value has changed to' + $('select.changeStatus').val());
    // You can perform an ajax request using the .ajax() method
    $.ajax({
          type: 'GET',
          url: 'your_script_python.py', // This is the url that will be requested

          // select value available inside your_script_python.py
          data: {selectFieldValue: $('select.changeStatus').val()},

          // on success: populate your second select.
          success: function(html){ 
            alert('data passed: ' + html);
           },
          dataType: 'html' // or json 
    });

});
});
</script>

$('document').ready(函数(){
//更改事件处理程序
$('select.changeStatus').change(函数(){
//可以使用.val()方法访问select字段的值
//警报('Select字段值已更改为'+$('Select.changeStatus').val());
//可以使用.ajax()方法执行ajax请求
$.ajax({
键入:“GET”,
url:'your_script_python.py',//这是将被请求的url
//选择\u脚本\u python.py中的可用值
数据:{selectFieldValue:$('select.changeStatus').val()},
//成功时:填充第二个选择。
成功:函数(html){
警报('传递的数据:'+html);
},
数据类型:“html”//或json
});
});
});

我知道SQL、HTML和Python之外还有其他东西。我是一个非常新的程序员,我没有任何Ajax方面的经验,但是,现在,我知道我需要阅读哪些教程。谢谢你的例子!
<script>
$('document').ready(function(){
//  change event handler
$('select.changeStatus').change(function(){
    // You can access the value of your select field using the .val() method
    //alert('Select field value has changed to' + $('select.changeStatus').val());
    // You can perform an ajax request using the .ajax() method
    $.ajax({
          type: 'GET',
          url: 'your_script_python.py', // This is the url that will be requested

          // select value available inside your_script_python.py
          data: {selectFieldValue: $('select.changeStatus').val()},

          // on success: populate your second select.
          success: function(html){ 
            alert('data passed: ' + html);
           },
          dataType: 'html' // or json 
    });

});
});
</script>