Uso uma função Javascript para adicionar options em um select. No Chrome funciona normal, mas no Firefox por uma fração de segundo vejo que a option foi adicionada, mas o navegador navega para uma página com o HTML abaixo, impedindo que continue utilizando a página.
[code]
false
</body>
[/code]
Só para esclarecer eu não criei nenhuma página Html assim, o Firefox está exibindo o retorno da função não sei porquê.
Alguém poderia me explicar o que está acontecendo? Porque só no Firefox isso acontece?
Segue o código da minha função e a chamada:
[code]function addOption(fonte, destino) {
select1 = document.getElementById(fonte);
select2 = document.getElementById(destino);
if (select1.options.length > 0 && select1.selectedIndex != -1) {
// adiciona todos os elementos selecionados do select1
// para o select2
while (select1.selectedIndex != -1) {
var selectedIndex = select1.selectedIndex;
var listText = select1.options[selectedIndex].text; // ome(name) do
// Elemento
var listValue = select1.options[selectedIndex].value;
var listTitle = select1.options[selectedIndex].title;
// Verifica se o elemento selecionado já foi adicionado anteriomente
for (k = 0; k < select2.options.length; k++) {
if (select2.options[k].value == listValue) {
// select1.options[selectedIndex] = null;
return true;
}
}
// envia o elemento do select1 para o 2
select2.options[select2.options.length] = new Option(listText,
listValue);
select2.options[select2.options.length - 1].title = listTitle;
select1.options[selectedIndex].selected = false;
}
}
return false;
}[/code]
Chamada da função, onde epesqregular e pesqgrupo são os id dos selects em questão:
<a href="javascript:addOption('epesqregular', 'pesqgrupo')">
<img src="../swf/insere.GIF" border="0" alt="inserir" title="Inserir" />
</a>
[quote=feandrad]Uso uma função Javascript para adicionar options em um select. No Chrome funciona normal, mas no Firefox por uma fração de segundo vejo que a option foi adicionada, mas o navegador navega para uma página com o HTML abaixo, impedindo que continue utilizando a página.
[code]
false
</body>
[/code]
Só para esclarecer eu não criei nenhuma página Html assim, o Firefox está exibindo o retorno da função não sei porquê.
Alguém poderia me explicar o que está acontecendo? Porque só no Firefox isso acontece?
Segue o código da minha função e a chamada:
[code]function addOption(fonte, destino) {
select1 = document.getElementById(fonte);
select2 = document.getElementById(destino);
if (select1.options.length > 0 && select1.selectedIndex != -1) {
// adiciona todos os elementos selecionados do select1
// para o select2
while (select1.selectedIndex != -1) {
var selectedIndex = select1.selectedIndex;
var listText = select1.options[selectedIndex].text; // ome(name) do
// Elemento
var listValue = select1.options[selectedIndex].value;
var listTitle = select1.options[selectedIndex].title;
// Verifica se o elemento selecionado já foi adicionado anteriomente
for (k = 0; k < select2.options.length; k++) {
if (select2.options[k].value == listValue) {
// select1.options[selectedIndex] = null;
return true;
}
}
// envia o elemento do select1 para o 2
select2.options[select2.options.length] = new Option(listText,
listValue);
select2.options[select2.options.length - 1].title = listTitle;
select1.options[selectedIndex].selected = false;
}
}
return false;
}[/code]
Chamada da função, onde epesqregular e pesqgrupo são os id dos selects em questão:
<a href="javascript:addOption('epesqregular', 'pesqgrupo')">
<img src="../swf/insere.GIF" border="0" alt="inserir" title="Inserir" />
</a>
[/quote]
<head>
<script>
// ver fonte da solução:
// http://stackoverflow.com/questions/128923/whats-the-effect-of-adding-return-false-to-an-onclick-event
function addOption(fonte, destino) {
select1 = document.getElementById(fonte);
select2 = document.getElementById(destino);
if (select1.options.length > 0 && select1.selectedIndex != -1) {
// adiciona todos os elementos selecionados do select1
// para o select2
while (select1.selectedIndex != -1) {
var selectedIndex = select1.selectedIndex;
var listText = select1.options[selectedIndex].text; // ome(name) do
// Elemento
var listValue = select1.options[selectedIndex].value;
var listTitle = select1.options[selectedIndex].title;
// Verifica se o elemento selecionado já foi adicionado anteriomente
for (k = 0; k < select2.options.length; k++) {
if (select2.options[k].value == listValue) {
// select1.options[selectedIndex] = null;
return true;
}
}
// envia o elemento do select1 para o 2
select2.options[select2.options.length] = new Option(listText,
listValue);
select2.options[select2.options.length - 1].title = listTitle;
select1.options[selectedIndex].selected = false;
}
}
return false;
}
</script>
</head>
<body style="margin: 10px;">
<select id="epesqregular">
<option>aaa1</option>
<option>aaa2</option>
</select>
<select id="pesqgrupo">
<option>Apple</option>
<option>Orange</option>
<option>Pineapple</option>
<option>Banana</option>
</select>
<a href="#" onclick="addOption('epesqregular', 'pesqgrupo')">Click here !</a> ---
<a href="javascript:addOption('epesqregular', 'pesqgrupo')"> inserir </a>
</body>