简体   繁体   English

重置使用jQuery选择框值

[英]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). 当我选择第一个选择框时,它会使用jQuery和AJAX(数据库中的值)自动加载第二个和第三个选择框的选项。

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. 我用empty()来清空选择框。 But it doesn't reset the value. 但它没有重置价值。

Instead of empty(), try something like this: 而不是empty(),尝试这样的事情:

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

See this post for more details 有关详细信息,请参阅此帖子

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

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