How to set the value of select option

Ferdinand Balbin

I am trying to populate the dropdown menu based on the selection of the first menu. using the code below it will just make the next dropdown to be empty

  $(\".category_id\").change(function(){

    $(\"#account_id > option\").remove();
    $(\"#item_name_id > option\").remove();

    var  category_id={'category_id':$(this).val()};

 $.ajax({
       type: \"POST\",
       url: 'getCategory1/', 
       dataType: \"json\",
       data: category_id,
       success: function(category_ids){ 

 // category_ids = {"0":"Choose Account Name","2":"OfficeEquipment","3":"IT Equipment"}
               $.each(category_ids,function(account_id,name){

                  var opt = $('<option />');           
                  opt.val(account_id);
                  opt.text(name);

                  $(this).closest('td').next().find('select').append(opt);

             });
          }

      });
});

The controller function I used:

  public function actionGetCategory1(){
        //Get all the sub categories a the main category
        $cat_id = $_POST['category_id'];
        $subCategory = Item::model()->getCategory1($cat_id); 
        echo(json_encode($subCategory));
    }

The model function

public function getCategory1($cat_id){
        $where  = "WHERE category_id = $cat_id";
        $select = "SELECT * FROM tbl_account $where";
        $query  =  Yii::app()->db->createCommand($select)->queryAll();

        $subCat = array();
        $subCat[0] = "Choose Account Name";
           if($query){
              foreach($query as $row){
                $subCat[$row['account_id']] = $row['account_name'];
              }
              return $subCat;
            }
            else{ return FALSE; }
    }

The data to be loop in the $.each will be coming form the controller in json format. I used var_dump to display i.

string(189) "{"0":"Choose Account Name","2":"Information and Communication Technology Equipment","3":"Office Equipment","4":"Furniture and Fixtures","5":"Communication Equipment","6":"Other Equipments"}"

Dropdown Form

James Grundner

Should be something like this. You will need to use a better selector for getting your < select > tag.

$.each(category_ids,function(account_id,name){
    var x = document.createElement('option');
    x.setAttribute('value', account_id);
    var y = document.createTextNode(name);
    $(x).append(y);

    $('#selectId').append(x)
});

EDIT After further discussion this looks like a better answer:

$(\".category_id\").change(function(){
    var _this = this;
    $(\"#account_id > option\").remove();
    $(\"#item_name_id > option\").remove();

    var  category_id={'category_id':$(this).val()};

 $.ajax({
       type: \"POST\",
       url: 'getCategory1/', 
       dataType: \"json\",
       data: category_id,
       success: function(category_ids){ 

 // category_ids = {"0":"Choose Account Name","2":"OfficeEquipment","3":"IT Equipment"}
               $.each(category_ids,function(account_id,name){

                  var opt = $('<option />');           
                  opt.val(account_id);
                  opt.text(name);

                  $(_this).closest('td').next().find('select').append(opt);

             });
          }

      });
});

This is something that trips up a lot of people. Here is a good quick read on "this" scope that should make this clearer to understand.http://javascriptplayground.com/blog/2012/04/javascript-variable-scope-this/

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to set the value of select option

From Dev

how to set a default value for select option

From Java

Set select option 'selected', by value

From Dev

Set value of Select option in Javascript

From Java

how to use ng-option to set default value of select element

From Dev

How do I to set selected value in a select option using jquery?

From Dev

How do I to set selected value in a select option using jquery?

From Dev

How can I set a select option value without using jQuery?

From Dev

How to set the selected option of <select>

From Dev

How to set the selected option of <select>

From Dev

Jquery how to select option with value

From Dev

How to select an option based on value

From Dev

Set default value of select to null option

From Dev

Set <select> value to a non-existent option

From Dev

jquery - Set select option based on json value

From Dev

Set <select> value to a non-existent option

From Dev

AngularJS : how to create select option and set default select option?

From Dev

How do you set <select> value/option in edit/new template - ExpressJS

From Dev

How to set value of the input when I use select option? Java script with if

From Dev

how to set select option if selected on submit in ci ,options value fetching from the database?

From Dev

how to set the value of an option when using ng-options in a select with angularjs

From Dev

how to set select option if selected on submit in ci ,options value fetching from the database?

From Dev

How do you set <select> value/option in edit/new template - ExpressJS

From Dev

How to set select option according to my vaildation

From Dev

How to set select option according to my vaildation

From Dev

How to set my default option select?

From Dev

How to write prompt to set default select option

From Dev

How to set default option in select with Angular?

From Dev

AngularJS - how to set default select option

Related Related

  1. 1

    How to set the value of select option

  2. 2

    how to set a default value for select option

  3. 3

    Set select option 'selected', by value

  4. 4

    Set value of Select option in Javascript

  5. 5

    how to use ng-option to set default value of select element

  6. 6

    How do I to set selected value in a select option using jquery?

  7. 7

    How do I to set selected value in a select option using jquery?

  8. 8

    How can I set a select option value without using jQuery?

  9. 9

    How to set the selected option of <select>

  10. 10

    How to set the selected option of <select>

  11. 11

    Jquery how to select option with value

  12. 12

    How to select an option based on value

  13. 13

    Set default value of select to null option

  14. 14

    Set <select> value to a non-existent option

  15. 15

    jquery - Set select option based on json value

  16. 16

    Set <select> value to a non-existent option

  17. 17

    AngularJS : how to create select option and set default select option?

  18. 18

    How do you set <select> value/option in edit/new template - ExpressJS

  19. 19

    How to set value of the input when I use select option? Java script with if

  20. 20

    how to set select option if selected on submit in ci ,options value fetching from the database?

  21. 21

    how to set the value of an option when using ng-options in a select with angularjs

  22. 22

    how to set select option if selected on submit in ci ,options value fetching from the database?

  23. 23

    How do you set <select> value/option in edit/new template - ExpressJS

  24. 24

    How to set select option according to my vaildation

  25. 25

    How to set select option according to my vaildation

  26. 26

    How to set my default option select?

  27. 27

    How to write prompt to set default select option

  28. 28

    How to set default option in select with Angular?

  29. 29

    AngularJS - how to set default select option

HotTag

Archive