简体   繁体   English

使用Ajax jQuery的动态选择选项不起作用

[英]Dynamic select option using Ajax jQuery not working

I am using Ajax jQuery to get select data with id "pSelId" and "pVim" from my Flask server (please see the HTML code below). 我正在使用Ajax jQuery从我的Flask服务器获取id为“pSelId”和“pVim”的选择数据(请参阅下面的HTML代码)。 This is working fine independently. 这个工作正常。

Now I have to take decision based on selected option in "pSelId" and return option data in select field "pVim", for which I have to get the value of "#p_name" on my Flask server from the selected value in select field "pSelId" to take this decision. 现在我必须根据“pSelId”中的选定选项做出决定并在选择字段“pVim”中返回选项数据,为此我必须从选择字段中的选定值获取Flask服务器上的“#p_name”值“ pSelId“做出这个决定。 Currently I am assigning static value for p_name='p15' on server side. 目前我在服务器端为p_name ='p15'分配静态值。

I have no idea how to send the value of selected option in 我不知道如何发送所选选项的值

Server side code: 服务器端代码:

@app.route('/listpopvim', methods=['POST','GET'])
def listpopvim():
    #p_name=request.form['pName'];
    p_name='p15'
    p_dict1 = []
    for p in load_p:
       p_dict2={
         'pop': p['fq_name'][1]
        }
       #pop_dict1.append(pop_dict2)

    return json.dumps(p_dict1)

HTML / jQuery: HTML / jQuery:

<div class="row">
     <div class="col-xs-4">
     <h6> P Name </h6>
     <select id="pSelId" name="pName"  class="form-control"> </option> </select>
     </div>

     <div class="col-xs-4">
     <h6> VIM </h6>
     <select id="pVim" name="pVim" class="form-control"> </select>
     </div>
</div>


<script>
getdata()
function getdata(){
              $.ajax({
                        url : '/listpv',
                        type : 'POST',
                        success: function(data){
                                var pretrun = JSON.parse(data);
                                console.log(data)
$( "select" ).change( displayVals );
        var options = "";
         for (var i = 0; i < pretrun.length; i++) {
           if (pretrun[i].pop){
                options += "<option>" + pretrun[i].pop + "</option>";
                      }
                } 
        $("#pSelId").html(options);
                        },
                        error: function(error){
                                console.log(error);
                        }
                });

}

function displayVals() {
        var singleValues = $( "#pSelId" ).val();
        console.log(singleValues) 
}
$( "#pSelId" ).change( displayVals );
displayVals();

</script>

I think all that you're missing here is the data field in your post request. 我想你在这里缺少的就是你的帖子请求中的数据字段。 You'll have to mess around a little bit with what you actually want in the data field because I'm not entirely sure that I understood exactly what you're looking for there, but this site should help with that. 你将不得不在数据领域中实际想要的东西搞乱一点,因为我不完全确定我完全理解你在那里寻找的东西,但这个网站应该有所帮助。

Python: 蟒蛇:

@app.route('/listpopvim', methods=['POST','GET'])
def listpopvim():
p_name=request.form['pName'];
p_dict1 = []
for p in load_p:
    p_dict2={
        'pop': p['fq_name'][1]
    }
   #pop_dict1.append(pop_dict2)

return json.dumps(p_dict1)

HTML: HTML:

<div class="row">
 <div class="col-xs-4">
 <h6> P Name </h6>
 <select id="pSelId" name="pName"  class="form-control"> </option> </select>
 </div>

 <div class="col-xs-4">
 <h6> VIM </h6>
 <select id="pVim" name="pVim" class="form-control"> </select>
 </div>
</div>

JS: JS:

<script>
getdata()
function getdata(){
    $.ajax({
        url : '/listpv',
        type : 'POST',
        data : {pName : $("pSelId").val()},
        success: function(data){
            var pretrun = JSON.parse(data);
            console.log(data);
            $( "select" ).change( displayVals );
            var options = "";
            for (var i = 0; i < pretrun.length; i++) {
                if (pretrun[i].pop){
                    options += "<option>" + pretrun[i].pop + "</option>";
                }
            } 
            $("#pSelId").html(options);
        },
        error: function(error){
            console.log(error);
        }
    });
}
function displayVals() {
    var singleValues = $( "#pSelId" ).val();
    console.log(singleValues) 
}
$( "#pSelId" ).change( displayVals );
displayVals();

</script>

Thank you for the reply. 感谢您的答复。 I did some modifications and was able to send data to server however i am not able to retrieve using request.form['pName']; 我做了一些修改,并能够将数据发送到服务器但是我无法使用request.form ['pName']进行检索; however i can see the data using below code ss=request.form print ss 但是我可以使用下面的代码ss = request.form print ss看到数据

another problem is this data is an ImmutableMultiDict, not sure why. 另一个问题是这个数据是一个ImmutableMultiDict,不知道为什么。 this is the output of print ss on my server console: 这是我服务器控制台上打印ss的输出:

ImmutableMultiDict([('pName', u'pop15')]) ImmutableMultiDict([('pName',u'pop15')])

Here is the modified code: 这是修改后的代码:

i get data for select1 from this function and result of which will be fwd to another function name displayVals on change event. 我从这个函数中获取select1的数据,其结果将转换为更改事件上的另一个函数名称displayVals。 second function will then populates my select2 field. 然后第二个函数将填充我的select2字段。

Function to get data for Select1 获取Select1数据的函数

                     function getpop(){
                          $.ajax({
                                url : '/listpopvim',
                                type : 'GET',
                                success: function(data){
                                     var pop = JSON.parse(data);
                                     console.log(data)
                                    var options = "";
                                    for (var i = 0; i < pop.length; i++) {
                                            if (pop[i].pop){
                                                    options += "<option>" + pop[i].pop + "</option>";
                                                    }
                                            } 
                                            $("#popSelId").html(options);
                                    $( "select" ).change( displayVals );
                                  },
                                  error: function(error){
                                        console.log(error);
                                    }
                            });

            }

Function to get data for Select2 函数获取Select2的数据

                    displayVals();
                    function displayVals() {
                            $.ajax({
                                    url: "/listpopvim",
                                    data: {pName: $( "#popSelId" ).val()},
                                    ContentType : 'application/json',
                                    dataType: "json",
                                    //data: JSON.stringify(singleValues),
                                    type:"POST",
                                    success : function(response) {
                                            //console.log(response)
                                            },
                                    error: function() {
                                            alert('Error occured');
                                            }
                                    });
                    }       

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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