简体   繁体   中英

Rails can placeholder text be submitted?

Can't find an answer anywhere in Docs... is there a way to make it so that the placeholder value for a number_field_tag or whatever is the value that is submitted to the value if the user doesn't enter anything else?

Ie, in my code below:

<%= number_field_tag "transaction[][#{thing}]", :quantity, min: 0, placeholder: @transactionparams ? @transactionparams["#{thing}"] : 0 %>

I would like the equation in placeholder to evaluate and then be POSTed if the user doesn't enter anything else.

Thanks!

No.

They will also automatically exclude the placeholder from being sent when the form is submitted.

You'll have to write some JavaScript to set the input's value property using its placeholder attribute, if its value is empty when the form is submitted.

HTML

The issue is HTML , not Rails

The problem is a placeholder is only there to give text that's visible only when the input has no value:

The placeholder attribute specifies a short hint that describes the expected value of an input field (eg a sample value or a short description of the expected format).

The short hint is displayed in the input field before the user enters a value.

This attribute (placeholder) does not store any data in the input, meaning when you submit the form, it's not going to send anything to your backend. To fix this, you should should switch from using the placeholder to value attribute:

<%= number_field_tag "transaction[][#{thing}]", :quantity, min: 0, value: @transactionparams ? @transactionparams["#{thing}"] : 0 %>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM