html下拉框选择后自动刷新,html select 下拉框刷新页面后保留上一次选择的值
常用場景
組合條件查詢點擊查詢/刷新頁面,包括input輸入框\復選框等在內的組件都可以通過前端設置value="{{id}}",后臺發送的數據包括對應的字段,從而實現刷新后保留上一次的值,提高用戶體驗,但是發現select不能這么做,它的value值是約定好并傳給后臺的,后臺可以傳回來,但無法直接顯示。
網上搜的資料五花八門,自己用jquery實現了,效果:
html代碼段
全國
北京
上海
廣州
深圳
杭州
蘇州
南京
成都
重慶
武漢
js
后臺是django,函數如下
def search_handle(request):
post = request.POST
input_content=post.get('input_content').strip()
input_cityid=post.get('input_cityid',0)
# print(input_cityid)
# print(input_content)
city='全國'
if input_cityid =='0':
city='全國'
params = ('%' + input_content + '%')
cursor = connection.cursor()
cursor.execute(
"SELECT * FROM `event` where eventTilte like %s" ,params )
else:
if input_cityid == '1':
city='北京'
elif input_cityid == '2':
city='上海'
elif input_cityid == '3':
city='廣州'
elif input_cityid == '4':
city='深圳'
elif input_cityid == '5':
city='杭州'
elif input_cityid == '6':
city='蘇州'
elif input_cityid == '7':
city='南京'
elif input_cityid == '8':
city='成都'
elif input_cityid == '9':
city='重慶'
elif input_cityid == '10':
city='武漢'
params = ('%' + input_content + '%','%' + city + '%')
cursor = connection.cursor()
cursor.execute(
"SELECT * FROM `event` where eventTilte like %s and eventAddress like %s" ,params )
event_list = cursor.fetchall()
cursor.close()
content = {'event_list':event_list,'input_content':input_content,'input_cityid':input_cityid}
return render(request,'c_activity/search.html',content)
另外還可以通過ajax來實現,后面再總結吧。。
總結
以上是生活随笔為你收集整理的html下拉框选择后自动刷新,html select 下拉框刷新页面后保留上一次选择的值的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: python基础入门(9)之字典
- 下一篇: oracle数据库各组件介绍,Oracl