如何在 Laravel Blade 模板中添加 input type="number"?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/35129359/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 13:09:14  来源:igfitidea点击:

How to add input type="number" in Laravel Blade template?

htmllaravellaravel-5blade

提问by user3506325

I want to make an input field where the user can only enter a number. In HTML5, we can use <input type="number">. How do I do this in blade?

我想制作一个用户只能输入数字的输入字段。在 HTML5 中,我们可以使用<input type="number">. 我如何在刀片中做到这一点?

I tried this:

我试过这个:

{!! Form::number('amount', null, array('class' => 'form-control')) !!}

回答by user3506325

I could search and code it. Since there was no direct answer, I thought to post the working code below:

我可以搜索和编码。由于没有直接答案,我想在下面发布工作代码:

{!! Form::input('number', 'amount', null, ['class' => 'form-control']) !!}

回答by smartrahat

You can use either Form::input()method or Form::number()method to achieve your goal.

您可以使用Form::input()方法或Form::number()方法来实现您的目标。

Form::input() method

表单::输入()方法

This method takes 4 arguments.

此方法需要 4 个参数。

  1. $type - (required) The first argument specifies the type of input. Values such as "text", "number", "password", "file", etc. are accepted.
  2. $name - (required) The second argument is name.
  3. $value - (optional) The third argument is the value for the input field.
  4. $options - (optional) The fourth argument is an array of additional field attributes. The array can be populated with items having keys such as "id", "size", or "class".
  1. $type -(必需)第一个参数指定输入的类型。接受诸如“文本”、“数字”、“密码”、“文件”等值。
  2. $name -(必需)第二个参数是名称。
  3. $value -(可选)第三个参数是输入字段的值。
  4. $options - (可选)第四个参数是附加字段属性的数组。该数组可以填充具有诸如“id”、“size”或“class”之类的键的项目。

Example:

例子:

{{ Form::input('number', 'name') }}
{{ Form::input('number', 'name', 'value', ['class' => 'number', 'id' => 'numberField']) }}

//both example will create elements like this

<input name="name" type="number" value="value">
<input name="name" type="number" value="value" class="number" id="numberField">

Form::number() method

表格::数字()方法

This method takes 3 arguments.

此方法需要 3 个参数。

  1. $name - (required) The first argument is name.
  2. $value - (optional) The second argument is the value for the input field.
  3. $options - (optional) The third argument is an array of additional field attributes. The array can be populated with items having keys such as "id", "size", or "class".
  1. $name -(必需)第一个参数是名称。
  2. $value -(可选)第二个参数是输入字段的值。
  3. $options -(可选)第三个参数是附加字段属性的数组。该数组可以填充具有诸如“id”、“size”或“class”之类的键的项目。

Example:

例子:

Form::number('name')
Form::number('name', null, ['class' => 'number', 'id' => 'numberField'])

//both example will create elements like this

<input name="name" type="number">
<input name="name" type="number" class="number" id="numberField">

Tips:if you want to use $optionsand don't want to assign any default value, use nullat the $valueargument.

提示:如果要使用$options且不想分配任何默认值,请使用nullat$value参数。

回答by markcanals

{!!  Form::input('number', 'weight', null, ['id' => 'weight', 'class' => 'form-control', 'min' => 1, 'max' => 9999, 'required' => 'required']) !!}

回答by Jay Vignesh

Input field with minimum value of 0.

最小值为 0 的输入字段。

{!! Form::number('count', $value = '' , ['min' => '0' ,'class' => 'form-control', 'id' => 'number_count','required']) !!}

回答by derrysan7

You can use javascript:

您可以使用 javascript:

function isNumberKey(evt){
    var charCode = (evt.which) ? evt.which : event.keyCode
    if (charCode > 31 && (charCode < 48 || charCode > 57))
        return false;
    return true;
}

and in your html:

并在您的 html 中:

<input name="input_name" onkeypress="return isNumberKey(event)">