根据另一个更改选择标记的选项

时间:2017-07-18 13:00:38

标签: jquery ajax

这是我的jqyery代码,我正在调用api并获得一些员工ID现在我想根据这些ID更改我的员工姓名的选项并删除所有其他基本上我只想要那些ID与那些ID匹配的名称



<script>


$(document).ready(function(){

$('#project_name').on('change', function() {
//  var values;
var item=$('#project_name :selected').val();
alert(item);
$.ajax({
   type: 'GET',
   url: 'http://localhost/programs/new/api/Project/get_employees_to_assinged_project?project_id='+item, //replace the plus by a comma
   success: function (data) {
      $.each(data, function(key,value){
    console.log(value.project_employee_id);
// //
// $("#employees_name option").filter('#'+value.project_employee_id,function() {
//     // return this.text == text1;
//  });
      });
            }
              });

});
});
</script>
&#13;
 <form method="get">
                    <button value="" name="datefilter" type="button" class="btn btn-default" id="daterange-btn">
                      <span>May 25, 2017 - June 7, 2017</span>
                      <i class="fa fa-caret-down"></i>
                    </button>
                    <select id="project_name" name="project_name" style="width:200px;" class="">
                      <option value="">No project</option>

                                        <option value="1">api</option>
                                      <option value="2">new project</option>
                                      <option value="3">new project2</option>
                  
                  </select>
                  <select name="employees_name" style="width:200px;" class="">
                    <option value="">No Employee</option>

                                    <option value="1">Administrator</option>
                                  <option value="2">sunitshakya</option>
                                  <option value="3">shikshasingh</option>
                                  <option value="255">abhishekshukla</option>
                
                </select>

                    <input type="hidden" name="start" id="start" value="">
                    <input type="hidden" name="end" id="end" value="">

                    <input class="btn btn-primary" type="submit" name="submit">
                 </form>
&#13;
&#13;
&#13;

&#13;
&#13;
<script>


$(document).ready(function(){

$('#project_name').on('change', function() {
//  var values;
var item=$('#project_name :selected').val();
alert(item);
$.ajax({
   type: 'GET',
   url: 'http://localhost/programs/new/api/Project/get_employees_to_assinged_project?project_id='+item, //replace the plus by a comma
   success: function (data) {
      $.each(data, function(key,value){
    console.log(value.project_employee_id);
// //
// $("#employees_name option").filter('#'+value.project_employee_id,function() {
//     // return this.text == text1;
//  });
      });
            }
              });

});
});
</script>
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:0)

获取选择的值时出错。尝试使用此代码并检查它是否有效。 ID应该成功传输。如果您收到错误,那么您的API会遇到一些问题:

&#13;
&#13;
$(document).ready(function(){

$('#project_name').on('change', function() {
//  var values;
var item=$('#project_name').val();
alert(item);
$.ajax({
   type: 'GET',
   url: 'http://localhost/programs/new/api/Project/get_employees_to_assinged_project?project_id='+item, //replace the plus by a comma
   success: function (data) {
      $.each(data, function(key,value){
    console.log(value.project_employee_id);
// //
// $("#employees_name option").filter('#'+value.project_employee_id,function() {
//     // return this.text == text1;
//  });
      });
            }
              });

});
});
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form method="get">
                    <button value="" name="datefilter" type="button" class="btn btn-default" id="daterange-btn">
                      <span>May 25, 2017 - June 7, 2017</span>
                      <i class="fa fa-caret-down"></i>
                    </button>
                    <select id="project_name" name="project_name" style="width:200px;" class="">
                      <option value="">No project</option>

                                        <option value="1">api</option>
                                      <option value="2">new project</option>
                                      <option value="3">new project2</option>
                  
                  </select>
                  <select name="employees_name" style="width:200px;" class="">
                    <option value="">No Employee</option>

                                    <option value="1">Administrator</option>
                                  <option value="2">sunitshakya</option>
                                  <option value="3">shikshasingh</option>
                                  <option value="255">abhishekshukla</option>
                
                </select>

                    <input type="hidden" name="start" id="start" value="">
                    <input type="hidden" name="end" id="end" value="">

                    <input class="btn btn-primary" type="submit" name="submit">
                 </form>
&#13;
&#13;
&#13;