简体   繁体   English

在使用Ajax和PHP更改多个select jQuery时

[英]On change on multiple select jquery with Ajax and PHP

I have a multiple selection in my HTML page which is generated in an AJAX response: the format is like this: 我在AJAX响应中生成的HTML页面中有多个选择:格式如下:

<div>
    <select class='myselect' idselect=1>
        <option value='private'>Private</option>
        <option value='public'>Public</option>
    </select><button idfeed=1 class='change'>Change status</button></div> 
<div>
    <select class='myselect' idselect=2>
        <option value='private'>Private</option>
        <option value='public'>Public</option>
    </select><button idfeed=2 class='change'>Change status</button>
</div>......

In my jquery/Ajax I have something like that (the code which generating the HTML above): 在我的jquery / Ajax中,我有类似的东西(上面生成HTML的代码):

<script>
$( document ).ready(function() {
    var id= $('#idUser').val(); 
    //console.log('findAll');
    $.ajax({
        type: 'GET',
        url: 'http://localhost/ws/api/getUserFeeds/'+id,
        dataType: "json", // data type of response
        success:function(data){ 
        //alert(data[0].URL);

            $.each(data, function (i) { 
                var toappend="<div><select class='myselect' idselect="+data[i].id+"><option value='private'>Private</option><option value='public'>Public</option></select><button idfeed="+data[i].id+" class='change'>Change status</button></div>";
                console.log(toappend);

                $("#foo").append(toappend+"<div id=rss"+i+ ">"+i+"</div>");
                $('#rss'+i).FeedEk({
                    FeedUrl: data[i].URL,
                    MaxCount: 1,
                    DateFormat: 'MM/DD/YYYY HH:mm'
                });

            });     
       }
    });

});
</script>

And when I want to retrive the value of selected by user which must either private or public I use this jquery code to select,but the problem is that it select only private all time. 当我想检索必须为私有或公共的用户选择的值时,我使用此jquery代码进行选择,但问题是它始终都仅选择私有。

<script type="text/javascript">
  $(document).on('change','.myselect',function(){
    var selectvar = $('.myselect').val();
    console.log(selectvar);
  });

</script>

As you can see I display in my console the value of the selected one,and using the JQuery change event. 如您所见,我在控制台中使用JQuery change事件显示所选值的值。 I try by many ways to try to make it work but it always return private(My select is composed of two choice:private and public) 我尝试通过多种方式尝试使其正常运行,但始终返回私有(我的选择由两个选择组成:私有和公共)

Thanks for your help!!! 谢谢你的帮助!!!

You made a small mistake at the end. 最后,您犯了一个小错误。 You should retrieve your select input with $(this) , not $('.myselect') : 您应该使用$(this)而不是$('.myselect')来检索select输入:

$(document).on('change','.myselect',function(){
    var selectvar = $(this).val();
    console.log(selectvar);
});

Try this 尝试这个

DEMO 演示

$(document).on('change','.myselect',function(){
    var selectvar = $(this).val();
    console.log(selectvar);
});

$(this) = > the element changed $(this)=>元素已更改

$(".myselect") => it can be any of the two select elements in your given html $(“。myselect”)=>它可以是给定html中的两个select元素中的任何一个

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

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