我有一个在列表框中调用的函数,该函数有一个ajax请求 要检索其他列表框的选项, 我试图解析来自ajax jquery的html响应,但它不起作用,只有响应数据'显示,我想在html响应中找到元素,这是我的代码
function loadOptionsValues(id) {
$.ajax(
{
type: 'POST',
url: baseDir + 'ajax.php',
data: 'val_select=' + $('#'+id).text()+'&id_product='+id_product,
dataType: "html",
success: function(data){
$( "select[id!='"+id+"']" ).each(function(){
$(this).html($(html).find('#'+$(this).attr('id')).text());
alert('text select'+$(html).find('#'+$(this).attr('id')).html());
});
}
});
}
响应看起来像这样
<html>
<select id="group_1">
<option id="Ecru">Ecru</option>
<option id="Gris">Gris</option>
<option id="Gris">Gris</option>
<option id="Gris">Gris</option>
<option id="Gris">Gris</option>
<option id="Marine">Marine</option>
<option id="Marine">Marine</option>
<option id="Marine">Marine</option>
<option id="Marine">Marine</option>
<option id="Marine">Marine</option>
<option id="Marine">Marine</option>
<option id="Marine">Marine</option>
<option id="Noir">Noir</option>
<option id="Noir">Noir</option>
<option id="Noir">Noir</option>
<option id="Noir">Noir</option>
<option id="Noir">Noir</option>
<option id="Noir">Noir</option>
<option id="Rose">Rose</option>
<option id="Rose">Rose</option>
<option id="Rose">Rose</option>
<option id="Rose">Rose</option>
<option id="Rose">Rose</option>
<option id="Rose">Rose</option>
<option id="Rose">Rose</option>
<option id="Taupe">Taupe</option>
</select>
<select id="group_3">
<option id="Cuir">Cuir</option>
<option id="Daim">Daim</option>
<option id="Daim">Daim</option>
<option id="Daim">Daim</option>
<option id="Daim">Daim</option>
<option id="Cuir">Cuir</option>
<option id="Cuir">Cuir</option>
<option id="Cuir">Cuir</option>
<option id="Cuir">Cuir</option>
<option id="Cuir">Cuir</option>
<option id="Cuir">Cuir</option>
<option id="Cuir">Cuir</option>
<option id="Cuir">Cuir</option>
<option id="Cuir">Cuir</option>
<option id="Cuir">Cuir</option>
<option id="Cuir">Cuir</option>
<option id="Cuir">Cuir</option>
<option id="Cuir">Cuir</option>
<option id="Cuir">Cuir</option>
<option id="Cuir">Cuir</option>
<option id="Cuir">Cuir</option>
<option id="Cuir">Cuir</option>
<option id="Cuir">Cuir</option>
<option id="Cuir">Cuir</option>
<option id="Cuir">Cuir</option>
<option id="Cuir">Cuir</option>
</select>
<select id="group_2">
<option id="40">40</option>
<option id="37">37</option>
<option id="40">40</option>
<option id="39">39</option>
<option id="38">38</option>
<option id="37">37</option>
<option id="41">41</option>
<option id="36">36</option>
<option id="40">40</option>
<option id="35">35</option>
<option id="39">39</option>
<option id="38">38</option>
<option id="40">40</option>
<option id="36">36</option>
<option id="39">39</option>
<option id="38">38</option>
<option id="41">41</option>
<option id="37">37</option>
<option id="38">38</option>
<option id="37">37</option>
<option id="41">41</option>
<option id="36">36</option>
<option id="40">40</option>
<option id="35">35</option>
<option id="39">39</option>
<option id="41">41</option>
</select>
</html>
答案 0 :(得分:1)
this
不再引用原始元素。您应该设置一个上下文变量:
function loadOptionsValues(id) {
var that = $(this);
$.ajax(
{
type: 'POST',
url: baseDir + 'ajax.php',
data: 'val_select=' + $('#'+id).text()+'&id_product='+id_product,
dataType: "html",
success: function(data){
$( "select[id!='"+id+"']" ).each(function(){
that.html($(html).find('#'+that.id).text());
alert('text select'+$(html).find('#'+that.id).html());
});
}
});
}