开发者

Another Basic Javascript Question

开发者 https://www.devze.com 2023-01-16 04:56 出处:网络
Ok, I have a jquery .post returning what I need it to, but it is not being output properly. Here is my code:

Ok, I have a jquery .post returning what I need it to, but it is not being output properly.

Here is my code:

jQuery(document).ready(function($){


 $('#project-for-support-ticket').change(function(){  
  //Pass The value selected to the get_users_from_project_id() function
  var projectSelected = $(this).val();
  if(projectSelected == 'NULL'){
   $('#users-of-project').empty();
  }

  $.post( '/wp-admin/admin-ajax.php', { 'selectedProject' :  projectSelected, 'action' : 'support_ticket_ajax'},
                function(data) {
     var newdata = jQuery.parseJSON( data );
     var value = 'ID';
     var text = 'name';
     returnOptions(newdata, '#users-of-project', value, text);
                }, "JSON"
            );

 })

 $('#user-to-assign-project').change(function(){
  var UserSelected = $(this).val();
  if(UserSelected == 'none'){
   $('#user-type-to-assign-as').empty();
  }

  $.post( '/wp-admin/admin-ajax.php', { 'UserSelected' :  UserSelected, 'action' : 'assign_project_ajax'},
                function(data) {
     var newdata = jQuery.parseJSON( data );
     var value = '';
     var text = '';
     returnOptions(newdata, '#use开发者_如何学Cr-type-to-assign-as', value, text);
                }, "JSON"
         );
 })

 function returnOptions(newdata, SelectToPopulate, value, text){
  //Empty the select box so we are not simply adding more to it!
  $(SelectToPopulate).empty();

  //Run a for loop, the closest thing javascript easily has to a foreach loop
  for ( var i in newdata ){
   if(!(value == '')){
    OptVal = newdata[i].value;
   }else{
    OptVal = newdata[i];
   }
   alert (OptVal);
   if(!(text == '')){
    OptText = newdata[i].text;
   }else{
    OptText = newdata[i];
   }

   //And Append an 'Option' to the select tag
   $(SelectToPopulate).append($('<option></option>').attr('value', OptVal ).text(OptText));
  }
 }
    })

The problem is in the returnOptions function, The user-to-assign-project is working perfectly, but for project-for-support-ticket I am getting 'undefined' when I alert OptVal, but if I alert newData[i].name it alerts the correct value.

Can anyone see what I am doing wrong??

Cheers Ben


you dont seem to have declared OptVal hence the undefined. in your function returnOptions start with the following line var OptVal; and then you should be able to alert OptVal without any problem.

0

精彩评论

暂无评论...
验证码 换一张
取 消