我有鑰匙,VAL dict
這樣的:試圖通過關鍵,VAL字典迭代,並與另一匹配鍵循環
choices = {'first': 1, 'second': 2, 'third': 3}
然後,像這樣一組的div:
<div class="choice_result">
<p class="choice_result_text">first<p/>
<span></span>
</div>
<div class="choice_result">
<p class="choice_result_text">second<p/>
<span></span>
</div>
<div class="choice_result">
<p class="choice_result_text">third<p/>
<span></span>
</div>
我想遍歷每個.choice_result_text
,如果這.choice_result_text
== key
,我想要將該span
的html更改爲val
。現在,我的jQuery代碼(在Ajax成功功能)看起來是這樣的:
result = $('.choice_result_text');
$.each(data.choices, function (key, val) {
$.each(result, function() {
if(result.html() == key) {
j = $('.choice_result').find('span').html(key);
j.html(val);
}
})
});
眼下,這個代碼變成每span
在選擇第一val
(1
)。任何想法如何使其正常工作?
爲什麼不與'choices'的關鍵標籤使用一個ID? –
不確定你的意思。例如, – Zorgan
'
first
''。 –