Ir para conteúdo

POWERED BY:

Arquivado

Este tópico foi arquivado e está fechado para novas respostas.

Fadweb

Criando botão de selecionar todos e de selecionar check box

Recommended Posts

Estou criando um sistema de disparo de emails em ASP e preciso criar um botao de selecionar todos e de selecionar os check box do formulario peguei um exemplo num forum em html ele fucinona normal mas coloquei no meu ssitema no comeco nao funcionou corretamente pois so seleciona 2 check box quando tem 4 e quando tem 2 nao seleciona nenhum

 

abaixo o codigo do script em HTML :

 

 

<html>
<head>
	<title>Formulário checkboxes</title>

<script type="text/JavaScript">

/*
- FUNCTION NUDACOR(ID_DA_LINHA), marca a área desejada quando se seleciona um checkbox, e também marca todas as áreas quando selecionado o checkbox "MESTRE"... Isso só funciona devido a função "getElementById()" que utilizei. A mesma trabalha junto com "id" da parte em html(<tr id=0>)....

- FUNCTION SELECIONA_TUDO(), esta por sua vez marca todos os checkbox existente no formulário quando selecionado o checkbox "MESTRE"...

*/

function mudaCor(id_da_linha){ 
	if (document.f0 == null){ //esta linha eu não entendi, na sorte funcionou
	   if (document.f0.mestre.checked == 1) //verifica se o checkbox mestre foi prescionado,
	 document.getElementById(id_da_linha).style.background='#FFFFD4' //caso sim, é destacado todas ás àreas 
 else  //selecionadas pelo checkbox mestre. Caso não o mesmo desmarca todos, tirendo o destaque colorido
	 document.getElementById(id_da_linha).style.background='' 
	}
	else {//caso o usuário apenas marque um dos checkbox simples, o mesmo é destacado.  
 if (document.getElementById(id_da_linha).style.background != '' ){
		 if (document.f1.elements[id_da_linha].checked == 1)
	   document.getElementById(id_da_linha).style.background='#FFFFD4'
		 else
		   document.getElementById(id_da_linha).style.background=''
 }	  
 else  
	   document.getElementById(id_da_linha).style.background='#FFFFD4'
	}
}

function selecionar_tudo(){ //função que seleciona todos os checkbox atraves do checkbox mestre...
  if (document.f1){	
	  for (var i = 0; i < document.f1.elements.length; i++) {
		if (document.f0.mestre.checked == 0) {
   document.f1.elements[i].checked = 0
   mudaCor(i)//tira o destaque de todos os checkbox caso o mestre seja desmarcado
  } 
		else { 
	   document.f1.elements[i].checked = 1
   mudaCor(i)//marca todos os checkbos e destaca todos....
  }
   }
  }
  
}
</script>


</head>
<body>
<form name="f0">
	<input type="checkbox" name="mestre" onClick="java script:selecionar_tudo()"> Marcar/Desmarcar todos
</form>

<form name="f1">
<table name="table" width="100%">
<tr id=0>
		<td><input type="checkbox" name="ch0" onClick="mudaCor('0')"> Opção 1 </td>
</tr>
<tr id=1>
		<td><input type="checkbox" name="ch1" onClick="mudaCor('1')"> Opção 2 </td>
</tr>
<tr id=2>
		<td><input type="checkbox" name="ch2" onClick="mudaCor('2')"> Opção 3 </td>
</tr>
<tr id=3>
		<td><input type="checkbox" name="ch3" onClick="mudaCor('3')"> Opção 4 </td>
</tr>
<tr id=4>
		<td><input type="checkbox" name="ch4" onClick="mudaCor('4')"> Opção 5 </td>
</tr>
</table>
</form>
</body>
</html>

AGORA O MEU CODIGO :

<%  
On Error Resume Next 
Dim conteudo
conteudo = Request.Form("conteudo")
Session("conteudo") = conteudo
if conteudo = "" then 
	conteudo = Session("conteudo")
end if 
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<!-- saved from url=(0014)about:internet -->
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>System for Web Solutions</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<style type="text/css">
td img {display: block;}body {
	background-color: #005C99;
	margin-left: 0px;
	margin-top: 0px;
	margin-right: 0px;
	margin-bottom: 0px;
}
.texto {font-family: Verdana; font-size: 10px; color: #000000; }
.titulo {	font-family: Verdana;
	font-size: 10px;
	color: #FFFFFF;
}
.style8 {font-family: Verdana; font-size: 10px; color: #FFFFFF; font-weight: bold; }
</style>
<!--Fireworks 8 Dreamweaver 8 target.  Created Mon Jun 15 14:44:46 GMT-0300 (Hora oficial do Brasil) 2009-->

</head>
<body>
<table width="875" border="0" align="center" cellpadding="0" cellspacing="0">
<!-- fwtable fwsrc="Contato Online.png" fwbase="conteudo.jpg" fwstyle="Dreamweaver" fwdocid = "1118439019" fwnested="0" -->
  <tr>
   <td><img src="images/spacer.gif" width="32" height="1" border="0" alt="" /></td>
   <td><img src="images/spacer.gif" width="23" height="1" border="0" alt="" /></td>
   <td><img src="images/spacer.gif" width="233" height="1" border="0" alt="" /></td>
   <td><img src="images/spacer.gif" width="508" height="1" border="0" alt="" /></td>
   <td><img src="images/spacer.gif" width="79" height="1" border="0" alt="" /></td>
   <td><img src="images/spacer.gif" width="1" height="1" border="0" alt="" /></td>
  </tr>

  <tr>
   <td colspan="5"><img name="conteudo_r1_c1" src="images/conteudo_r1_c1.jpg" width="875" height="5" border="0" id="conteudo_r1_c1" alt="" /></td>
   <td><img src="images/spacer.gif" width="1" height="5" border="0" alt="" /></td>
  </tr>
  <tr>
   <td colspan="5"><img name="conteudo_r2_c1" src="images/conteudo_r2_c1.jpg" width="875" height="119" border="0" id="conteudo_r2_c1" alt="" /></td>
   <td><img src="images/spacer.gif" width="1" height="119" border="0" alt="" /></td>
  </tr>
  <tr>
   <td colspan="5"><img name="conteudo_r3_c1" src="images/conteudo_r3_c1.jpg" width="875" height="15" border="0" id="conteudo_r3_c1" alt="" /></td>
   <td><img src="images/spacer.gif" width="1" height="15" border="0" alt="" /></td>
  </tr>
  <tr>
	<td colspan="5" bgcolor="#FFFFFF"> </td>
	<td><img src="images/spacer.gif" width="1" height="28" border="0" alt="" /></td>
  </tr>
  <tr>
   <td rowspan="3"><img name="conteudo_r5_c1" src="images/conteudo_r5_c1.jpg" width="32" height="50" border="0" id="conteudo_r5_c1" alt="" /></td>
   <td colspan="2" bgcolor="#FFFFFF"> </td>
   <td rowspan="3" colspan="2"><img name="conteudo_r5_c4" src="images/conteudo_r5_c4.jpg" width="587" height="50" border="0" id="conteudo_r5_c4" alt="" /></td>
   <td><img src="images/spacer.gif" width="1" height="20" border="0" alt="" /></td>
  </tr>
  <tr>
   <td colspan="2"><img name="conteudo_r6_c2" src="images/conteudo_r6_c2.jpg" width="256" height="10" border="0" id="conteudo_r6_c2" alt="" /></td>
   <td><img src="images/spacer.gif" width="1" height="10" border="0" alt="" /></td>
  </tr>
  <tr>
   <td colspan="2" bgcolor="#FFFFFF"> </td>
   <td><img src="images/spacer.gif" width="1" height="20" border="0" alt="" /></td>
  </tr>
  <tr>
   <td colspan="5"><img name="conteudo_r8_c1" src="images/conteudo_r8_c1.jpg" width="875" height="31" border="0" id="conteudo_r8_c1" alt="" /></td>
   <td><img src="images/spacer.gif" width="1" height="31" border="0" alt="" /></td>
  </tr>
  <tr>
   <td colspan="2" background="images/conteudo_r9_c1.jpg"> </td>
   <td colspan="2" bgcolor="#EFEEE9" valign="top"><table width="100%" border="0" cellspacing="0" cellpadding="0">
	 <tr></tr>
	 <tr>
	   <td></td>
	 </tr>
	 <tr></tr>
	 <tr>
	   <td><%

response.Write (Session("conteudo"))
response.Write ("</br>")
'response.Write (conteudo)
'response.End()

Dim Tipo_usuario, Idioma, nome, nascimento
Dim tipo_criterio, criterio
	   
Tipo_usuario = Request.Form("Tipo_usuario")
Idioma = Request.Form("Idioma")
nome   = Request.Form("nome")
nascimento = Request.Form("nascimento")
'response.write(Tipo_usuario)
'response.write("</br>")
'response.write(Idioma)
'response.write("</br>")
'response.write(nome)
'response.write("</br>")
'response.write(nascimento)
'response.write("</br>")
'response.end()
 if Tipo_usuario <> "" then 
tipo_criterio  = "Tipo_usuario"
criterio = Tipo_usuario
end if 

if Idioma <> "" then 
tipo_criterio  = "Idioma"
criterio = Idioma
end if 

if nome <> "" then 
tipo_criterio  = "nome"
criterio = nome
end if 

if nascimento <> "" then 
tipo_criterio  = "nascimento"
criterio = nascimento
end if 
'Aqui vamos abrir a conexão para resgatar valores do banco de dados
strCon = "DBQ=d:\web\localuser\wizardpirituba\www\dados\emailmkt.mdb;Driver={Microsoft Access Driver (*.mdb)};"
Set objRS = Server.CreateObject("ADODB.Recordset")
objRS.CursorLocation = 3
objRS.CursorType = 0
objRS.LockType = 1
'if tipo_criterio <> "" then 
if tipo_criterio = tipo_criterio  then 
strSQL = "SELECT * FROM TB_USERS where Unidade ='Chacara Inglesa' AND " & tipo_criterio & "= '" & criterio  & "'"
else   
strSQL = "SELECT * FROM TB_USERS where Unidade ='Chacara Inglesa'"
end if 

if tipo_criterio = ""   then
strSQL = "SELECT * FROM TB_USERS where Unidade ='Chacara Inglesa'"
'response.end()
end if 
objRS.Open strSQL, strCon
If not objRS.EOF then 
On Error Resume Next 
%>

		   <table width="100%" border="0" cellpadding="0" cellspacing="0">
			 <tr></tr>
			 <tr>
			   <td></td>
			 </tr>
			 <tr></tr>
			 <tr>
			   <td><p>
				 <%
'Aqui faremos o action apontando para a página de envio.

%>
			   <strong>Filtro de Selecão				 </strong></p>
				 <form id="filtro" name="filtro" method="post" action="selecionar_emails.asp">
				   <p><strong>Pesquisar Por					</strong>: </p>
				   <table width="297" border="0" cellspacing="0" cellpadding="0">
					 <tr>
					   <td width="172"><strong>Tipo de 
						usuário</strong></td>
					   <td width="125"><select name="Tipo_usuario" id="Tipo_usuario">
						 <option value="">Selecione</option>
						 <option value="Aluno">Aluno</option>
						 <option value="Ex-aluno">Ex-aluno</option>
						 <option value="Outros">Outros</option>
											  </select></td>
					  </tr>
					 <tr>
					   <td><div align="center"></div></td>
					   <td> </td>
					  </tr>
					 <tr>
					   <td><strong>Idioma Interessado </strong></td>
					   <td><select name="Idioma" id="Idioma">
						 <option value="">Selecione</option>
						 <option value="Ingles">Ingles</option>
						 <option value="Espanhol">Espanhol</option>
						 <option value="Frances">Frances</option>
						 <option value="Alemao">Alemao</option>
						 <option value="Italiano">Italiano</option>
						 <option value="Chines">Chines</option>
						 <option value="Japones">Japones</option>
						 <option value="Portugues">Portugues</option>
																	 </select></td>
					  </tr>
					 <tr>
					   <td><div align="center"></div></td>
					   <td> </td>
					  </tr>
					 <tr>
					   <td><strong>Nome</strong></td>
					   <td><input name="nome" type="text" id="nome" /></td>
					  </tr>
					 <tr>
					   <td><div align="center"></div></td>
					   <td> </td>
					 </tr>
					 <tr>
					   <td><strong>Data de Nascimento </strong></td>
					   <td><input name="nascimento" type="text" id="nascimento" /></td>
					 </tr>
				   </table>
				   <p>
					 <input type="hidden" name="conteudo" value='<%=conteudo%>' />
				   </p>
				   <p>
					 <input type="submit" name="Submit2" value="Pesquisar" />
				  </form>
				 <form name="f0" id="f0">
				   <input type="checkbox" name="mestre" onclick="java script:selecionar_tudo()" />
					Marcar Todos / Desmarcar todos
				   
				 </form>
				 <p> </p>
				 <form action="set.asp" method="post" name="f1" id="f1">
					 <p align="center">Assunto
					   <input name="assunto" type="text" id="assunto" />
					 </p>
					<p align="center">Unidade
					   <select name="unidade" id="unidade">
						   <option value="atendimentopsd@wizardpirituba.com.br">Pq. São Domingos</option>
						   <option value="atendimento@wizardpirituba.com.br">Chacará Inglesa</option>
					   </select>
					</p>
					<p align="center">
					  <input name="Submit" type="submit" class="texto" value="Enviar" />
					</p>
					<p align="left"><strong>Selecione abaixo os destinatários :</strong></p>
					<table width="100%" border="0" cellpadding="2" cellspacing="1">
					 <tr>
						 <td width="20" height="20" align="center" bgcolor="#006699" ><span class="titulo">></span></td>
						 <td width="50%" height="20" bgcolor="#006699"><span class="style8">Nome</span></td>
						 <td width="50%" height="20" bgcolor="#006699"><span class="style8">E-mail</span></td>
					   </tr>
					   <%
Dim tr_id
Dim valor_cor
Dim var_nameBox
tr_id = 0
valor_cor = 0
var_nameBox = 0 
'Aqui faremos a alternação das linhas da tabela, lembrando que não devemos colocar <tr> fora da linha ASP. 
objRS.MoveFirst
While Not objRS.EOF
if cor ="#FFFFFF" then
cor = "#CCCCCC"
else
cor ="#FFFFFF"
end if
'Wresponse.Write "<tr id=" & tr_id & ">"
%>
					   <tr id=<%=tr_id%>>
						 <td width="20" align="center"><p class="texto">
						   <%
'Passamos valor do e-mail para checkbox que separa valores escolhidos por ",".
%>
						   <input name="box" type="checkbox" onClick="mudaCor('<%=valor_cor%>')" value="<%= objRS("email")%>"/>
						   <% response.Write(tr_id)
						   response.Write("<br/>")
						   response.Write(valor_cor)
							'response.Write("<br/>")
						   'response.Write(var_nameBox)
						   %> 
						 </p>
						 </td>
						 <%
'Listaremos nome no recordset.
%>
						 <td width="50%" class="texto" ><%=objRS("nome")%></td>
						 <%
'Listaremos e-mail no recordset.
%>
						 <td width="50%" class="texto"><%=objRS("email")%></td>
					   </tr>
					   <%
tr_id = tr_id + 1
valor_cor = valor_cor + 1 
var_nameBox = var_nameBox + 1 
objRS.MoveNext
Wend 
%>
					</table>
				  </form></td>
			 </tr>
		 </table>
		 <%
Else
response.Write("Não exixte o que você procura  <a href='java script:history.go(-1)'>voltar </a>")
End If
%>
		 </td>
	 </tr>
   </table></td>
   <td background="images/conteudo_r9_c5.jpg"> </td>
   <td><img src="images/spacer.gif" width="1" height="217" border="0" alt="" /></td>
  </tr>
  <tr>
   <td colspan="5"><img name="conteudo_r10_c1" src="images/conteudo_r10_c1.jpg" width="875" height="14" border="0" id="conteudo_r10_c1" alt="" /></td>
   <td><img src="images/spacer.gif" width="1" height="14" border="0" alt="" /></td>
  </tr>
  <tr>
   <td colspan="5"><img name="conteudo_r11_c1" src="images/conteudo_r11_c1.jpg" width="875" height="123" border="0" id="conteudo_r11_c1" alt="" /></td>
   <td><img src="images/spacer.gif" width="1" height="123" border="0" alt="" /></td>
  </tr>
  <tr>
   <td colspan="5"><img name="conteudo_r12_c1" src="images/conteudo_r12_c1.jpg" width="875" height="5" border="0" id="conteudo_r12_c1" alt="" /></td>
   <td><img src="images/spacer.gif" width="1" height="5" border="0" alt="" /></td>
  </tr>
</table>
</body>
</html>
<script type="text/JavaScript">
function mudaCor(id_da_linha){ 
	if (document.f0 == null){ //esta linha eu não entendi, na sorte funcionou
	   if (document.f0.mestre.checked == 1) //verifica se o checkbox mestre foi prescionado,
	 document.getElementById(id_da_linha).style.background='#FFFFD4' //caso sim, é destacado todas ás àreas 
 else  //selecionadas pelo checkbox mestre. Caso não o mesmo desmarca todos, tirendo o destaque colorido
	 document.getElementById(id_da_linha).style.background='' 
	}
	else {//caso o usuário apenas marque um dos checkbox simples, o mesmo é destacado.  
 if (document.getElementById(id_da_linha).style.background != '' ){
		 if (document.f1.elements[id_da_linha].checked == 1)
	   document.getElementById(id_da_linha).style.background='#FFFFD4'
		 else
		   document.getElementById(id_da_linha).style.background=''
 }	  
 else  
	   document.getElementById(id_da_linha).style.background='#FFFFD4'
	}
}

function selecionar_tudo(){ //função que seleciona todos os checkbox atraves do checkbox mestre...
  if (document.f1){	
	  for (var i = 0; i < document.f1.elements.length; i++) {
		if (document.f0.mestre.checked == 0) {
   document.f1.elements[i].checked = 0
   mudaCor(i)//tira o destaque de todos os checkbox caso o mestre seja desmarcado
  } 
		else { 
	   document.f1.elements[i].checked = 1
   mudaCor(i)//marca todos os checkbos e destaca todos....
  }
   }
  }
  
}
</script>
<%
objRS.close
%>

Compartilhar este post


Link para o post
Compartilhar em outros sites

×

Informação importante

Ao usar o fórum, você concorda com nossos Termos e condições.