• 0
Sign in to follow this  
msb_gj

Checks

Question

Boa Tarde

estou precisando de algo assim:

tenho uma série de checkbox e ao lado um textbox , no qual deverá ser de preenchimento obrigatório quando o checkbox for selecionado...

Por favor como eu poderia fazer isso galera?

Fiz algo assim:

<script language="JavaScript">

function desabilita() {d = document.form1;

if (document.form1.chk1.checked) {

alert("O campo " + d.txt1.name + " deve ser preenchido!");

document.form1.txt1.disabled = false;

d.txt1.focus(); }

else

{ document.form1.txt1.disabled = true; }

if (document.form1.chk2.checked) {

alert("O campo " + d.txt2.name + " deve ser preenchido!");

document.form1.txt2.disabled = false;

d.txt2.focus(); }

else

{ document.form1.txt2.disabled = true;}

}

</script>

só que quando checko o segundo ele dá o alerta do primeiro tb....

Obrigado

MSB_GJ

Share this post


Link to post
Share on other sites

5 answers to this question

Recommended Posts

  • 0

segue o form:

<form name="form1" action="res.php" method="post" target="a">

<table width="560">

<tr>

<td width="356"><font size="1" face="Verdana, Arial, Helvetica, sans-serif">

<input name="chk1" type="checkbox" id="chk1" value="2" onclick="desabilita()">

Desmoronamento</font></td>

<td width="25"><font size="1" face="Verdana, Arial, Helvetica, sans-serif">50%<? echo "R$". number_format( cobertura(2),2,',','.');?></font></td>

<td width="163"><font size="1" face="Verdana, Arial, Helvetica, sans-serif">

<input name="txt1" type="text" id="txt1" onKeydown="FormataMoeda(this,10,event)" onkeypress="return maskKeyPress(event)">

</font> </td>

</tr>

<tr>

<td height="22"> <font size="1" face="Verdana, Arial, Helvetica, sans-serif">

<input name="chk2" type="checkbox" id="chk2" value="3" onclick="desabilita()" >

Alagamento</font></td>

<td><font size="1" face="Verdana, Arial, Helvetica, sans-serif">20%<? echo "R$". number_format( cobertura(3),2,',','.');?></font></td>

<td><font size="1" face="Verdana, Arial, Helvetica, sans-serif">

<input name="txt2" type="text" id="txt2" onKeydown="FormataMoeda(this,10,event)" onkeypress="return maskKeyPress(event)">

</font></td>

</tr>

<tr>

<td><font size="1" face="Verdana, Arial, Helvetica, sans-serif">

<input name="chk3" type="checkbox" id="chk3" value="4">

Danos El&eacute;tricos</font></td>

<td><font size="1" face="Verdana, Arial, Helvetica, sans-serif">50%<? echo "R$". number_format( cobertura(4),2,',','.');?></font></td>

<td><font size="1" face="Verdana, Arial, Helvetica, sans-serif">

<input name="txt3" type="text" id="txt3" onKeydown="FormataMoeda(this,10,event)" onkeypress="return maskKeyPress(event)">

</font></td>

</tr>

</table>

<input type="submit" name="Submit" value="Submit">

</form>

Obrigado

MSB_GJ

Share this post


Link to post
Share on other sites
  • 0

fiz uns testes aqui, você teria que fazer uma função para habilitar e desabilitar os campos e outra para fazer a validação dos campos caso eles estejam habilitados.

abraços

Share this post


Link to post
Share on other sites
  • 0

fercosmig

Teria como você me dar uma mãozinha com essa função, porque não entendo quase nada se javascript

Obrigadão

MSB_GJ

Share this post


Link to post
Share on other sites
  • 0

<html>

<head>

<script language="JavaScript" type="text/javascript">

<!--

function desab(){

if (document.getElementById('a5').disabled == true){

document.getElementById('a5').disabled = false;

document.getElementById('a6').disabled = false;

}

else {

document.getElementById('a5').disabled = true;

document.getElementById('a6').disabled = true;

}

}

function valid(){

campos=a1.Name.value;

if (campos=='') {

alert('Campo Obrigatório');

event.returnValue=false;

}

}

//-->

</script>

</head>

<body>

<FORM NAME="a1" METHOD="post" onsubmit="valid();">

<input type="checkbox" name="a2" value="a3" id="a4" onclick="desab();">Clique aqui<br>

<input type="text" name="pe_de_pano" id="a5" disabled="disabled">

<input type="submit" name="a6" id="a6" value="Vai" disabled="disabled">

</form>

</body>

</html>

Cara fiz este codigo ae! mas não testei!

da uma olhada ae!

abraços

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Answer this question...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Sign in to follow this