问题描述
<select class="txtbx1" name="country" disabled>
<option value='FR' >FRANCE</option><option value='CH' selected>SWITZERLAND</option>
</select>
上面的代码在一个方法是post的表单中
the above code is inside a form whose method is post
但是 echo $_POST['country']
没有显示任何东西..另一方面,如果我从 select $_POST['country']
中删除 disabled正确的结果
but echo $_POST['country']
is showing nothing.. on the other hand if I remove disabled from select $_POST['country']
is showing the correct result
推荐答案
这就是 disabled
属性的工作原理.当表单控件被禁用时,提交表单时将忽略该值,并且键不会出现在 $_POST
(或 $_GET
)中.
This is how the disabled
attribute works. When a form control is disabled, the value will be ignored when the form is submitted and the key will not be present in $_POST
(or $_GET
).
如果您希望该值出现在提交的数据中,但又不希望用户能够更改页面上的值(我想这就是您想要实现的),请使用 readonly="readonly"
而不是 disabled="disabled"
.
If you want the value to be present in the submitted data, but you don't want the user to be able to change the value on the page (which I imagine is what you are trying to acheive) use readonly="readonly"
instead of disabled="disabled"
.
编辑
元素没有
readonly
属性.上述信息仍然将适用于s和
s.
The <select>
element does not have a readonly
attribute. The above information still stands as it will work for <input>
s and <textarea>
s.
此处问题的解决方案是禁用选择并使用隐藏输入将值发送回服务器 - 例如
The solution to your problem here would be to disable the select and use a hidden input to send the value back to the server - e.g.
启用选择时:
<select class="txtbx1" name="country">
<!-- options here -->
</select>
...当它被禁用时:
<select class="txtbx1" name="country_disabled" disabled="disabled">
<!-- options here, with appropriate value having `selected="selected"` -->
</select>
<input type="hidden" name="country" value="value_of_field" />
这篇关于$_POST 禁用选择的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!