0

我有两个下拉列表。如果用户只从一个选择框中选择某些内容,我想重新呈现具有相同下拉列表的页面,除了选择的页面显示上一个选择而不是默认选择。

html看起来像

<div>
<select name = "menu" value = "{{menu}}">
    <option value = "selected">select something</option>
    <option value = "a">a</option>
    <option value = "b">b</option>
    <option value = "c">c</option>
</select>
</div>

<div>
<select name = "location" value = "{{location}}">
    <option value = "selected">select something</option>
    <option value = "d">d</option>
    <option value = "e">e</option>
    <option value = "f">f</option>
</select>
</div>

python 看起来像 menu = self.request.get('menu') location = self.request.get('location')

    if menu!="selected" and location!="selected":
        d = Delivery(parent = delivery_key(),
                 menu = menu, location = location)
        d.put()
        deliverystatus = "success"
        self.render('page.html',
                     deliverystatus = deliverystatus, menu = menu, location = location)
    else:
       error = "please select something from both lists"
       self.render("page.html",
                   menu = menu, location = location, error = error)

当出现错误并重新呈现页面时,我在两个框中都将“选择某些内容”作为默认值。我该如何改变?

4

1 回答 1

0

在 HTML 表单中,在选项中添加if menu_value==this_option_value打印条件。selected所以是这样的:

<select name = "menu" value = "{{menu}}">
    <option value = "selected">select something</option>
    <option value = "a" <% if menu == 'a': %>selected<% end> >a</option>
    <option value = "b" <% if menu == 'b': %>selected<% end> >b</option>
    <option value = "c" <% if menu == 'c': %>selected<% end> >c</option>
</select>

您应该检查语法,但这应该让您知道如何去做。

于 2013-11-07T11:15:42.410 回答