How to disable the submit button if the text field is empty in ruby โ€‹โ€‹on rails?

I am new to ROR. I just want to know if the send tag button can be disabled if the text box is empty.

thanks

+4
source share
3 answers

You can do this with jquery, for example,

Live demo

if($('#text_field').val() == "") $('#submitButtonId').attr('disabled', true); $('#text_field').keyup(function(){ if($('#text_field').val() != "") $('#submitButtonId').attr('disabled', false); else $('#submitButtonId').attr('disabled', true); }); 

For the latest version of jQuery, you may need to use prop () instead of attr () to set the disabled property of the element.

 if($('#text_field').val() == "") $('#submitButtonId').prop('disabled', true); 
+13
source

This is usually done using checks, so the button remains active, but the form receives validation errors and does not save. In your model, you would add:

 validates_presence_of :some_field, :some_other_field 

If you still want to do this, you would use javascript to execute it.

+1
source

You can use jQuery for this

 $(function(){ var val = $('#text_field').val(); if(val == ''){ $('input[type=submit]').attr('disabled', true) } }); 

Tick โ€‹โ€‹FIDDLE

0
source

Source: https://habr.com/ru/post/1435542/


All Articles