【问题标题】:Multiple html select and jquery ajax多个 html 选择和 jquery ajax
【发布时间】:2010-11-10 18:58:55
【问题描述】:

我想用 ajax 和 jquery 创建一个多重

First select(c1) 工作正常。当我点击它返回另一个选择(c2)。但是当我点击第二个选择(c2)时,我可以获得第三个选择(c3)。

HTML:

<table><tr><td id="cat1div">
<select name="c1">
<option value="">---</option>
<option value="1">Auto</select>
<option value="2">Moto</select>
</td>
<td id="cat2div"></td>
<td id="cat3div"></td>
</tr></table>

jquery:

$(document).ready(function() {
$("select[name=c1]").change(function () {
var id = $("select[name=c1] option:selected").val();

$.ajax({
            url: "category.php?cat=2&id=" + id, 
            cache: false,
            success: function (html) {
                $('#cat2div').html(html);
            }
        });

}); 



$("select[name=c2]").change(function () {
alert('asad');
var id = $("select[name=c2] option:selected").val();

$.ajax({
            url: "category.php?cat=3&id=" + id, 
            cache: false,
            success: function (html) {
                $('#cat3div select').html(html);
            }
        });

}); 

});

PHP:

<?if($_GET['cat']==2){?>
<select name="c2">
<option value="">---</option>
<?foreach($cat2[$_GET['id']] as $ca => $key){
?><option value="<?=$ca;?>"><?=$key;?><?}?>
</select>
<?}elseif($_GET['cat']==3){?>
<select name="c3">
<option value="">---</option>
<?foreach($cat3[$_GET['id']] as $ca => $key){
?><option value="<?=$ca;?>"><?=$key;?><?}?>
</select>
<?

抱歉我的英语不好,感谢您的帮助

【问题讨论】:

    标签: php jquery ajax select


    【解决方案1】:

    这应该可以解决问题:

    $(document).ready(function() {
      $("select[name=c1]").change(function () {
        $.ajax({
          url: "category.php?cat=2&id=" + $(this).val(), 
          cache: false,
          success: function (html) {
            $('#cat2div').html(html).find("select[name=c2]").change(function () {
              $.ajax({
                url: "category.php?cat=3&id=" + $(this).val(), 
                cache: false,
                success: function (html) {
                  $('#cat3div select').html(html);
                }
              }); 
            });
          }
        });
      }); 
    });
    

    目前,当$("select[name=c2]") 运行时,该元素还不存在...所以没有什么可以将change 处理程序绑定到...一旦元素存在,您需要这样做> 在那里,就像我上面所说的那样。

    【讨论】:

    • 我不懂 PHP,但在我看来,他(或她)生成 &lt;select&gt; 元素的那些循环总是在每个 &lt;option&gt; 之后包含 &lt;/select&gt; ...
    猜你喜欢
    • 2020-11-28
    • 2018-05-16
    • 1970-01-01
    • 2015-07-13
    • 1970-01-01
    • 1970-01-01
    • 2014-10-14
    • 2013-03-18
    • 1970-01-01
    相关资源
    最近更新 更多