简体   繁体   中英

Reset Select box values using jQuery

I have three select boxes. When I select first select box,it automatically loads options of second and third select box using jQuery and AJAX (values from database).

But when I change the option of second select box,it should list the third select box values based on second select box option.But now I am getting select box options for third as per first option.

$(document).ready(function()
{
  $("#cat").change(function()
  {
    var pc_id = $(this).val();
    if(pc_id != '')  
     {
      $.ajax
      ({
         type: "POST",
         url: "load_sub_cats.php",
         data: "catid="+ pc_id,
         success: function(option)
         {
           $("#subcat").html(option);
         }
      });
     }
     else
     {
       $("#subcat").html("<option value=''>-- No category selected --</option>");
     }
 if(pc_id != '')  
          {
           $.ajax
           ({
              type: "POST",
              url: "load_qset.php",
              data: "catid="+ pc_id,
              success: function(option)
              {
                $("#questionset").html(option);
              }
           });
          }
          else
          {
            $("#questionset").html("<option value=''>-- No question set selected--</option>");
          }
    return false;
  });

$("#subcat").change(function()
      {

        var pc_id = $(this).val();

        //$("#questionset").html("<option value=''>-- Select Question set --</option>");
    $("#questionset").empty();
     if(pc_id != '')  
                      {
                       $.ajax
                       ({
                          type: "POST",
                          url: "load_subcat_qset.php",
                          data: "subcatid="+ pc_id,
                          success: function(option)
                          {
                            $("#questionset").html(option);
                          }
                       });
                      }
                      else
                      {
                        $("#questionset").html("<option value=''>-- No question set selected --</option>");
                      }
        return false;
      });
    });`

I have used empty() to empty the select box. But it doesn't reset the value.

Instead of empty(), try something like this:

$('#questionset')
    .find('option')
    .remove()
    .end()
    .append('<option value="whatever">text</option>')
    .val('whatever')
;

See this post for more details

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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