Reset button on the form

Asked

Viewed 373 times

0

I want to reset all fields of a form using Cakephp framework.

What happens is the following, the sign up button is only enabled if the user marks the checkbox to accept.

If he clicks the clear button, I want him to leave the fields of type empty text and also reset the radio and checkbox.

The way I’m doing it only works to reset the type text, but I want to reset all kinds of fields in the form. Let’s go to part of it.

echo $this->Form->create('users');

echo $this->Form->input('name', array('label'=>'login'));

echo $this->Form->input('password', array('label'=>'senha'));

//checkbox para abilitar o cadastro

echo $this->Form->input('aceitar', array('label'=>'aceito realizar cadastro', 'name'=>'aceitar', 'onclick'=>'cadastrar.disabled!=checked'));

//botao cadastrar

echo $this->Form->submit('cadastrar', array('label'=>'Cadastrar', 'name'=>'cadastrar', 'onclick'=>'disabled=true'));

//reset

//da forma abaixo só desabilita o botão cadastrar e o checkbox continua marcado.

echo $this->Form->button('limpar', array('label'=>'limpar', 'onclick'=>'cadastrar.disabled=true'));

//ou desmarca o checkbox e o botão fica disponível.

echo $this->Form->button('limpar', array('label'=>'limpar', 'onclick'=>'aceitar.checked=false'));

The two forms above do not correspond to me.

I want to cancel the checkbox and disable the sign-up button, already tried using two onclick on reset button, but did not work.

3 answers

2


You can use a function in javascript to clear the fields

$(function(){
    $('#aceitar').on('click', function(){
       $('#enviar').prop('disabled', !$(this).is(':checked'));
    });

    $('#limpar').on('click', function(){
       $('#aceitar').prop('checked', false); 
       $('#enviar').prop('disabled', true);
       $('#nome').val(''); 

    });
});

Example: jsfiddle

1

You can use pure Javascript to solve your problem by being very simple.

Example:

document.getElementById("myForm").reset();

0

You can add a type input reset, it will reset the form to its default values, example:

Form:

 <form name="myform" method="POST">
  <input type="text" size="25" value="">
  <input type="text" size="25" value="Valor default">
  <input type="checkbox">
  <input type="radio">
  <input type="submit" value="enviar"> 
  <input type="reset" value="Resetar!">

</form>

  • I do this, but using framework. You did what I do, but in html. It didn’t clear up my question!

  • The code does exactly what you want, but in the proper way, the difference and it does not use javascript as the answer you marked as correct, its justification is not valid.

  • @Andrénascimento I understand that David’s answer does not satisfy you, but the problem is that in the title and the question you only ask to reset the form and David answered exactly this, but when reading your code I see that it goes beyond resetting, you want to reset and disable in certain circumstances, however you did not describe this in the question and of course most could not understand the objective, I recommend that when you ask a question follow the tips: http://answall.com/help/how-to-Ask and http://answall.com/help/mcve ...

  • @Maybe if you had used at least the tag javascript in the question David and the others could have deduced this, but since you did not, I think it wrong to criticize David’s answer, it is perfectly appropriate to what is described, so always try to be the clearest to avoid invalid answers. A good night! I hope you take all this as constructive tips :)

  • @Davidschrammel +1 for his effort, as the answer at the moment answers what is described in the question.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.