2010-08-16 90 views
0

我正在我們的網站上進行車輛搜索,您選擇make和ajax加載模型並填充選擇框。問題是我無法使用選項更新選擇框。我已經嘗試了jQuery('> .model', container).replaceWith(html);select.html(html);,兩者都不起作用。jQuery .html()沒有更新?

的HTML是這樣的...

<div class='vehicle-search'> 
    <select class='make'><-- options filled when page loads --></select> 
    <select class='model'></select> 
    <select class='year'></select> 
</div> 

的jQuery:

jQuery().ready(function() { 
jQuery('.vehicle-search .make').bind('click', function (e) { 
    var makeId = jQuery(e.target).val(); 
    var container = jQuery(e.target).parent('.vehicle-search'); 
    var select = jQuery('> .model', container); 
    if (parseInt(makeId) > 0) { 
    jQuery.ajax({ 
    url: iKeyLess.internal.url + '/ajax/vehicle/make/getModelList.php', 
    type: 'post', 
    dataType: 'json', 
    success: function (r) { 
    if (r.length > 0) { 
     select.html(''); 
     jQuery('> .year', container).html(''); 

     var html = ''; 
     for (var i = 0; i < r.length; i++) { 
     html += "<option value='"+r[i].id+"'>"+r[i].name+"</option>"; 
     } 

     jQuery('> .model', container).replaceWith(html); 
    } else { 
     alert('We did not find any models for this make'); 
    } 
    }, 
    error: function() { 
    alert('Unable to process your request, ajax file not found'); 
    return false; 
    }, 
    data: { 
    makeId: makeId 
    } 
    }); 
    } else { 
    select.html(''); 
    jQuery('> .model', container).html(''); 
    jQuery('> .year', container).html(''); 
    } 
}); 
}); 

回答

3

你的選擇不應該>開始。
因此,它們不匹配任何東西。

+0

我看到一個jQuery文檔的評論,有人曾經推薦過這個解決方案。顯然這是錯誤的^ _^ – Webnet 2010-08-16 16:59:16

+0

@Webnet - 當你傳遞'container'作爲上下文時,你可以這樣做'> .model'。這是一個例子:http://jsfiddle.net/Kf4hu/ – user113716 2010-08-16 17:00:13

+0

@帕特里克 - 這就是我做的,它不會工作 – Webnet 2010-08-16 17:07:25

0

使用jQuery('vehicle-container > select.model').html('...')