开发工具最有用的功能之一是,当你写一个函数的名字时,你可以找回它的源代码。以下是selectCountry
函数的源代码:
function selectCountry(select) {
if (select.value == "000") return;
var option = select.options[select.selectedIndex];
var ul = select.parentNode.getElementsByTagName('ul')[0];
var choices = ul.getElementsByTagName('input');
for (var i = 0; i < choices.length; i++)
if (choices[i].value == option.value) {
$("#selcountry:selected").removeAttr("selected");
$('#selcountry').val('[]');
return;
}
var li = document.createElement('li');
var input = document.createElement('input');
var text = document.createTextNode(option.firstChild.data);
input.type = 'hidden';
input.name = 'countries[]';
input.value = option.value;
li.appendChild(input);
li.appendChild(text);
li.onclick = delCountry;
ul.appendChild(li);
addCountry(option.firstChild.data, option.value);
$("#selcountry:selected").removeAttr("selected");
$('#selcountry').val('');
}
您的缺陷现在很明显。 selectCountry
接受整个选择元件作为参数而不是在选择的价值(这是一种可怕的设计,但MEH)。相反,传递元素的价值,改变它的指数:
var sel = document.getElementById('selcountry');
var opts = sel.options;
for(var i = 0; i < opts.length; i++) {
sel.selectedIndex = i
selectCountry(sel)
}
我真的希望这是一个测试代码或类似的东西,你就不是建立在这个产品/工具... – Codeman
请澄清只是那是什么不起作用。一个jsfiddle也是有帮助的。 –