我有以下代码,我想略微操纵它,但在普通的javascript中这样做更熟悉:
jQuery ->
states = $('#person_state_id').html()
console.log(states)
$('#person_country_id').change ->
country = $('#person_country_id :selected').text()
options = $(states).filter("optgroup[label=#{country}]").html()
console.log(options)
if options
$('#person_state_id').html(options)
else
$('#person_state_id').empty()
谢谢!
答案 0 :(得分:4)
嗯,对我来说,用“普通javascript”字样描述的代码看起来应该是这样的:
var states = document.getElementById('person_state_id').cloneNode(true);
document.getElementById('person_country_id').onchange = function() {
var country = this.value;
var childs = states.childNodes;
for (var i in childs) {
if (childs[i].label == country) {
var options = childs[i].innerHTML;
document.getElementById('person_state_id').innerHTML = options ? options : "";
}
}
};
查看此处的示例:http://jsfiddle.net/LmWqA/
答案 1 :(得分:2)
jQuery(function() {
var states;
states = $('#person_state_id').html();
console.log(states);
return $('#person_country_id').change(function() {
var country, options;
country = $('#person_country_id :selected').text();
options = $(states).filter("optgroup[label=" + country + "]").html();
console.log(options);
if (options) {
return $('#person_state_id').html(options);
} else {
return $('#person_state_id').empty();
}
});
});
答案 2 :(得分:0)
这是一个方便的JS to Coffee转换器,我经常使用,效果很好。