简体   繁体   English

文本输入值作为数字

[英]Text input value as a number

I want input field value to be a number instead of string. 我希望输入字段值是数字而不是字符串。

A simple scenario is you have 2 input fields and submit button on a page. 一个简单的场景是您在页面上有2个输入字段和Submit按钮。 When you click submit you should get sum of numbers keyed in both the input fields and not appended strings. 当您单击提交时,您应该获得在两个输入字段中键入的数字总和,而不是附加字符串。

I tried using "number_field_tag" for input type=number but the value is still a String and not Fixnum what I want. 我尝试将“ number_field_tag”用于输入type = number,但该值仍然是String而不是我想要的Fixnum。

As Surya stated in his comment, what comes from the form fields is always a string. 正如Surya在他的评论中所述,来自表单字段的内容始终是字符串。 But you can do something like this in the controller action that processes the form (presuming you're using Rails): 但是您可以在处理表单的控制器操作中执行以下操作(假设您使用的是Rails):

def process_form
  @result = params[:first_field].to_i + params[:second_field].to_i
end

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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