doupin5408 2012-12-07 05:38
浏览 73

jQuery动态下拉框ajax

I currently have two dropdown boxes and a textbox.

I am using a database query to fill the first dropdown box and ajax to fill the second dropdown, depending on the value chosen in the first dropdown. I also am using ajax to fill in the input textbox once a selection from the second dropdown is chosen.

My problem is, with my current code, when I choose an option from the first dropdown, although it DOES fill in the second dropdown box with the data returned from ajax, I also want it to ajax the input box as well and return that value at the same time as the second dropdown value updates.

     $( "#manufacturerOpts" ).change(function(){
            val1 = $(this).attr("value");
            $.ajax({
               url: "inc/class/data.php",
               type: "POST", 
               data: "manu="+val1,
               cache: false,
               success: function (html1) {   

               $('#modelOpts').html(html1);

               val2 = $("#modelOpts option").attr("value");

              $.ajax({
               url: "inc/class/data.php",
               type: "POST", 
               data: "mod="+val2,
               cache: false,
               success: function (html2) {  
               $('#powerOpts').html(html2);    
               }
              });
                }
              });

    });

    $( "#modelOpts" ).change(function(){
            val1 = $(this).attr("value");
            $.ajax({
               url: "inc/class/data.php",
               type: "POST", 
               data: "mod="+val1,
               cache: false,
               success: function (html1) {  $('#powerOpts').attr("value",html1);  }
              });

            // $("#modelOpts").selectmenu('refresh', true);
    });
  • 写回答

1条回答 默认 最新

  • dongliyan7318 2012-12-07 05:56
    关注

    You can modify your request to send the value for the textbox along with the dropdown menu options

    $.ajax({
        url: "inc/class/data.php",
        type: "POST", 
        data: "manu="+val1,
        dataType: 'json',
        success: function (data) {
            $('#modelOpts').html(data.modelOpts);
            $('#powerOpts').html(data.powerOpts);    
        }
    });
    

    and in you server-side code you can easily find out the value of the first option for the modelOpts dropdown and send it as well

    $data = array('modelOpts' => modelOptsHtml, 'powerOpts' => powerOptsHtml);
    echo json_encode($data);
    
    评论

报告相同问题?

悬赏问题

  • ¥15 组策略中的计算机配置策略无法下发
  • ¥15 机器学习简单问题解决
  • ¥15 如何绘制动力学系统的相图
  • ¥15 对接wps接口实现获取元数据
  • ¥20 给自己本科IT专业毕业的妹m找个实习工作
  • ¥15 用友U8:向一个无法连接的网络尝试了一个套接字操作,如何解决?
  • ¥30 我的代码按理说完成了模型的搭建、训练、验证测试等工作(标签-网络|关键词-变化检测)
  • ¥50 mac mini外接显示器 画质字体模糊
  • ¥15 TLS1.2协议通信解密
  • ¥40 图书信息管理系统程序编写