Django - 在选择标签中获取未使用的/所有选项 [英] Django - Get unused/all options in a select tag

查看:206
本文介绍了Django - 在选择标签中获取未使用的/所有选项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用POST表单,但我不知道这是否是最好的方式。



我有这个:

 < form name ='main 'method =POSTaction = {{submit}}> 
< select class ='form-control'size = 10 id =testname ='test'multiple>
< option> Test1< / option>
< option> Test2< / option>
< option> Test3< / option>
< option> Test4< / option>
< / select>
< input type =submit/>
< / form>

如果我使用request.POST.getlist('test'),我只得到突出显示。但是,我希望选择标签中的所有选项。



ex。

  def myapp_submit(request):
#This方法不存在
打印请求.POST.getalloptions

这将给我所有选项,无论是否被选择。

  ['Test','Test2','Test3','Test4'] 



这个?



我正在使用Jquery使用两个选择列表,您可以从一个列表中选择选项并将其放在另一个列表中。当用户提交时,我想知道哪些选项保留在每个选择列表中。



更新 -



Adam Brenecki说这个方法永远不会存在。我可以做的是使用JQuery遍历所有的列表,并更改所选的所有选项。但是,我可以帮助吗?

  //如何将所有选项更改为选定的
$( '#TEST')ChangeOptionToSelected。


解决方案

没有这样的选项存在,因为没有发送数据首先到服务器;只有选定的值是。



由于您已经在客户端使用JavaScript,因此您可以以编程方式填写隐藏的表单字段,或将您想要的值发送到另外要考虑的是,你想要有两个列表,一个'选择'和'未选择的'一个可视化



,但在概念上你正在做的是从列表中挑选多个项目,这是一个单一的< select multiple /> 是为。



最好将底层表单字段设为单个< select multiple /> 正常工作,然后在JavaScript中构建您想要的UI,将视图中的原始表单字段隐藏并更新。这样,JavaScript关闭的访问者仍然会得到一些有用的东西,而且可能会更容易使其访问。


I'm using a POST form, but I'm not sure if that is the best way.

I have this:

<form name='main' method="POST" action={{submit}}> 
<select class='form-control' size=10 id="test" name='test' multiple>
<option>Test1</option>
<option>Test2</option>
<option>Test3</option>
<option>Test4</option>
</select>
<input type="submit"/>
</form>

If I use request.POST.getlist('test'), I get only the options that are highlighted. However, I would like all the options that are in the select tag.

ex.

def myapp_submit(request):
    #This method doesn't exist
    print request.POST.getalloptions

which would give me all options regardless if they are selected or not.

['Test','Test2','Test3','Test4']

why would I do this?

I'm using Jquery to use two select lists that you can select options from one list and put it in another. When the user submits I want to know which options remain in each select list.

Update -

Adam Brenecki says this method will never exist. What I can do is iterate through all my lists using JQuery and change all the options that are there to selected. However, can I have some help with this?

    //How can I change all options to selected
    $('#test').ChangeOptionToSelected;

解决方案

No such option exists because that data isn't sent to the server in the first place; only the selected values are.

Since you're using JavaScript on the client side already, you could programmatically fill a hidden form field, or send the values you want to the server some other way.

The other thing to think about is, you want to have two lists, a 'selected' and 'unselected' one visually, but conceptually what you're doing is picking multiple items from a list, which is what a single <select multiple /> is for.

It might be best to have the underlying form field be a single <select multiple /> that works normally, then build up the UI you want in JavaScript, hide the original form field from view, and update it. That way, visitors with JavaScript turned off are still going to get something that works, and it's probably going to be easier to make it accessible too.

这篇关于Django - 在选择标签中获取未使用的/所有选项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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