如何POST空< select .. multiple> HTML元素是否为空? [英] How to POST empty <select .. multiple> HTML elements if empty?

查看:178
本文介绍了如何POST空< select .. multiple> HTML元素是否为空?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在HTML表单中有以下多选框,用户可以选择一个或多个选项。

 << ; select id =eng_0name =eng_0 []multiple size =3> 
< option value =Privilégier>Privilégier< / option>
< option value =Accepté>Accepté< / option>
< option value =Temporaire> Temporaire< / option>
< / select>

当用户不选择任何选项时,表单会被POST到PHP后端,但它不会创建空数组值为$ _POST ['eng_0'],就好像这个字段甚至不在表单中。



这就好比未选中的复选框没有提交问题。 / p>

即使没有选择选项,是否有任何方法可以使选择对象POST?如果你的表单是动态生成的,你可以在表单中添加一个隐藏的表单元素包含虚拟值的同名。然后,如果您为该变量获得的值是 ['dummy_value'] ,那么只需忽略虚拟值,那么您可以将其视为代码中的没有选定内容。

I have the following multi-select box in a HTML form, where user can select one or more option.

<select id="eng_0" name="eng_0[]" multiple size="3">
  <option value="Privilégier">Privilégier</option>
  <option value="Accepté">Accepté</option>
  <option value="Temporaire">Temporaire</option>
</select>

When the user selects no option, the form is POSTed to a PHP backend but it creates no empty array value for $_POST['eng_0'] as if the field was not even on the form.

This is kind of like the unchecked checkbox that is not submitted problem.

Is there any way to have it POST the select object even if there is no selected option? It can be in jQuery if that helps.

解决方案

If your form is generated dynamically, you could include a hidden form element with the same name that contains a dummy value. Then, just ignore the dummy value, if the value you get for that variable is ['dummy_value'] then you can treat that as meaning "nothing selected" in your code.

这篇关于如何POST空&lt; select .. multiple&gt; HTML元素是否为空?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆