I'm wondering if there is a simple way to reset the values of a form that had already been used, using javascript. I had tried to make a function that would set the values to an "empty string" (not sure if that's what it's called in this case, but '') and then to their default values, but this didn't work. And I had tried form reset, but that seemed to delete my form. I would be okay with restoring default values except I couldn't figure out how to set the radio button to NULL or nothing.
Thanks for any suggestions.
You can use <input type="reset" />[MDN], which will render a button that resets the form back to the state it was in when it was loaded.
Here's an example: http://jsfiddle.net/zEYEa/embedded/result/
Related
What I want
When I submit a form, this specific field must to turn empty if the value is invalid.
I have this function that validate the value and set it empty if necessary.
The problem
When I try to submit the form with an invalid value the function clears the field and it seems to work, but if I click again to submit the form the old value (that doesn't apears in the field anymore) kinda bypasses the validation and is inserted into the database.
The val('') is cleaning the field but somehow the value remains.
The code
Here is the code I am using:
if (response==true) {
$("#license").val('');
}
What I already tried
document.getElementById('license').value = "" // didn't work at all
$("#license").val(' '); // with one space but I lose the placeholder visualization, and I need it
Could anyone help me? Thanks!
Good night guys, I have a very simple angular question. I have a form, it has a specific field that I set the value for a patch value but at the same time I need to disable this field. However after the disable, the value is not sent to the back end to be inserted anymore. I've done the test and I saw that only with the patch value, the value is sent normally and inserted in backend normally, but when I disable it, it loses the value and is not sent to the back, I already tried onlyself but I didn't succeed , I would like your help if possible, thanks for your attention. Ah I know that html disable works, but I would like to know if there is any way for me to do this in typescript by disabling the field in the form.
iniciarSituacaoCadastro(){
if(this.visualizar){
this.formPesquisar.controls['address'].patchValue(Address.ATIVO);
this.formPesquisar.controls['address'].disable({onlySelf: true});
}
}
Use getRawValue() instead of value.
https://angular.io/api/forms/FormGroup#getRawValue
getRawValue() retrieves all values regardless of disabled status. The
value property is the best way to get the value of the group, because
it excludes disabled controls in the FormGroup.
I am looking for a way with my form I am currently showing and hiding fields based on the values selected in the dropdowns, What I want to know is.
when i select yes and the field below displays I click submit on the form, if I return to the form the value is still present but the field is hidden again...
How can I prevent that from happening by default?
I want my browser to remember the jQuery change funtions state I left it at after I submit the form.
What you want to do is 'refresh fields visibility' in some cases. I suggest you to create such function refreshFieldsVisibility. Such function reads values from the dropdown and shows/hide the proper field. Then call your function:
When elements state is changed, with on('change') events.
When document is ready (this is your case as I understand), with $(document).ready
Any other situation if necessary
Firstly, I have the jQuery set up so the form doesn't submit: http://jsfiddle.net/FztLS/1/
Only it doesn't submit if the fields are set. I was wondering if anyone knew what was going wrong in the script.
I assumed that the return false; would only apply if the field was empty.
Also, how do I apply the class to every empty field, not just the first?
Lastly, is there a better way to include the radio box validation?
Update your else clause to:
else {
$('#respond input, #respond textarea').removeClass('form_error');
$('#commentform').submit();
}
Here's the fiddle: http://jsfiddle.net/FztLS/5/
The 'submit' event can only be bound to a form element, you're attempting to bind it to a section.
FYI, $("section#respond") is bad practice. You should never have more than one element with the same id per page, all sorts of things will go wrong. This will suffice: $("#respond").
The problem is here:
if ($('input[name="rating"]:not(:checked)').val()) {
Since these are radio buttons there will always be unchecked ones so you always get a value and therefore return false.
Try this instead:
if($('input[name="rating"]:checked').length === 0) {
Which means "If no radio button is checked".
Thanks to everyone that helped, but I ended up fixing my own problem: http://jsfiddle.net/yBmdx/20/
Turns out that starting from scratch, and setting up an if($validForm) was the best method.
I have input field
<input type="text" name="vehicle_make[]" id="make1"/>
and i have help dropdown that updates this field if user choose to do so. I do it trough standard jquery code
$("#make1").val("value");
Problem is, since i use validate plugin to validate this field, if user click on validate before entering anything in that box, he will get notice that he needs to fill it, but then if he fills it trough dropdown, validate plugin will not detect it until user click on submit again.
I know i could call submit in function in which i process dropdown, but i think that it is not right solution, since i would need to check if validation is already done before doing that (since if it is not, it would start validation before i want it to start).
I also need to tie some other things to that field also, so i would like to know is there is a way in which i could write function so it always check if field is filled, even if user did not fill it directly.
Sorry if i didn't explain everything right, this is my first message here.
Try this
$("#make1").change(function(){
//do something there
});
I have found solution. First, i created js variable form_submitted, and added onclick event to submit button, to change value of variable form_submitted to yes. Then, i created function:
function update_validation(){
if(form_submitted == 'yes'){
$("#my_form").valid();
};
};
that i call when user do something that is not detected regularly with validate plugin. This function manually starts validation again, but only if it has been started before by clicking on submit.