HTML 中的复选框的 value 属性是什么意思?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14323899/
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
What does the value attribute mean for checkboxes in HTML?
提问by Metalcoder
Suppose this checkbox snippet:
假设这个复选框片段:
<input type="checkbox" value="1">Is it worth?</input>
Is there any reason to statically define the value
attribute of checkboxes in HTML? What does it mean?
是否有任何理由value
在 HTML 中静态定义复选框的属性?这是什么意思?
采纳答案by user1116033
I hope I understand your question right.
我希望我理解你的问题。
The value attribute defines a value which is sent by a POST request (i.e. You have an HTML form submitted to a server). Now the server gets the name (if defined) and the value.
value 属性定义了一个由 POST 请求发送的值(即您有一个 HTML 表单提交给服务器)。现在服务器获取名称(如果已定义)和值。
<form method="post" action="urlofserver">
<input type="checkbox" name="mycheckbox" value="1">Is it worth?</input>
</form>
The server would receive mycheckbox
with the value of 1
.
服务器将收到mycheckbox
值为1
。
in PHP, this POST variable is stored in an array as $_POST['mycheckbox']
which contains 1
.
在 PHP 中,这个 POST 变量存储在一个数组中,$_POST['mycheckbox']
其中包含1
.
回答by Nift
I just wanted to make a comment on Adriano Silva's comment. In order to get what he describes to work you have to add "[]" at the end of the name attribute, so if we take his example the correct syntax should be:
我只想对阿德里亚诺席尔瓦的评论发表评论。为了让他描述的东西起作用,你必须在 name 属性的末尾添加“[]”,所以如果我们以他的例子为例,正确的语法应该是:
<input type = "checkbox" name="BrandID[]" value="1">Ford</input>
<input type = "checkbox" name="BrandID[]" value="2">GM</input>
<input type="checkbox" name="BrandId[]" value="3">Volkswagen</input>
Then you use something like: $test = $_POST['BrandID']; (Mind no need for [] after BrandID in the php code). Which will give you an array of values, the values in the array are the checkboxes that are ticked's values.
然后你使用类似的东西: $test = $_POST['BrandID']; (注意在 php 代码中 BrandID 之后不需要 [])。这将为您提供一个值数组,数组中的值是勾选值的复选框。
Hope this helps! :)
希望这可以帮助!:)
回答by Adriano Silva
One reason is to use the ease of working with values ??in the system.
原因之一是利用系统中值的易用性。
<input type="checkbox" name="BrandId" value="1">Ford</input>
<input type="checkbox" name="BrandId" value="2">GM</input>
<input type="checkbox" name="BrandId" value="3">Volkswagen</input>
回答by Alf Eaton
When the form is submitted, the data in the value
attribute is used as the value of the form input if the checkbox is checked. The default value is "on".
提交value
表单时,如果复选框被选中,则属性中的数据将用作表单输入的值。默认值为“开”。
$('form').on('change', update).trigger('change')
function update() {
var form = $(this)
form.find('output').text('→ ' + form.serialize())
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form>
<input type="checkbox" name="foo">
<output></output>
</form>
<form>
<input type="checkbox" name="foo" checked>
<output></output>
</form>
<form>
<input type="checkbox" name="foo" value="1" checked>
<output></output>
</form>
<form>
<input type="checkbox" name="foo" value="bananas" checked>
<output></output>
</form>