JSF - usando getElementById

3 respostas
A

Não estou conseguindo pegar o valor de um campo usando getElementById:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml"
	xmlns:h="http://java.sun.com/jsf/html">

<h:head>
	<title>Gerador de Apostas</title>
		<script language="JavaScript">
		
		function doValidation(){
			int i = document.getElementById("form1:qtdeNum").value; 
			alert(i);
		}
	</script>
</h:head>
<h:body>
	<h:form id="form1">
		<h:panelGrid>
			<h:outputLabel value="Quantidade total de números:" for="qtdeNum"></h:outputLabel>
			<h:inputText value="#{geradorDeApostasBean.quantidadeDeNumeros}" id="qtdeNum"></h:inputText>

			<h:outputLabel value="Quantidade de números por aposta:"></h:outputLabel>
			<h:inputText value="#{geradorDeApostasBean.tamanhoDaAposta}"></h:inputText>

			<h:outputLabel value="Quantidade de apostas:"></h:outputLabel>
			<h:inputText value="#{geradorDeApostasBean.quantidadeDeAposta}"></h:inputText>

			<h:commandButton action="#{geradorDeApostasBean.geraApostas}"
				value="Gerar" onclick="doValidation();"></h:commandButton>			
		</h:panelGrid>
	</h:form>
</h:body>
</html>

3 Respostas

dssimao

Cara, tente usar somente o valor do id:

int i = document.getElementById("qtdeNum").value;
A

Já havia tentado e nada.

D

Tenta assim:

Primeira opção:

int i = document.getElementById(“form1:qtdeNum”).value;

Id do formulário + : + id do componente.

Segunda opção:

Adicione prependId=false no formulário.

Assim: <h:form id=form1 prependId=false >

Em seguida use seu codigo normal.

Assim: int i = document.getElementById(qtdeNum).value;

O prependId não deixa que o jsf concatene o id do formulário com o id do componente para formar o id do componente final.
Criado 29 de dezembro de 2010
Ultima resposta 29 de dez. de 2010
Respostas 3
Participantes 3