欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Popup彈出框添加數(shù)據(jù)實(shí)現(xiàn)方法

 更新時(shí)間:2017年10月27日 09:14:36   作者:Ayhan_huang  
這篇文章主要為大家詳細(xì)介紹了Popup彈出框添加數(shù)據(jù)的簡(jiǎn)單實(shí)現(xiàn)方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

本文實(shí)例為大家分享了Popup彈出框添加數(shù)據(jù)的具體代碼,供大家參考,具體內(nèi)容如下

邏輯

窗口P1中顯示一組數(shù)據(jù),并提供一個(gè)添加按鈕
點(diǎn)擊按鈕,彈出新的瀏覽器窗口P2,在其中添加一條數(shù)據(jù)并提交后,窗口P2自動(dòng)關(guān)閉
新添加數(shù)據(jù)動(dòng)態(tài)添加到窗口P1中并被選中
所需知識(shí):JS BOM 窗口對(duì)象;JS自執(zhí)行函數(shù)

實(shí)現(xiàn)

下面在Django中簡(jiǎn)單實(shí)現(xiàn)下,因?yàn)楸容^簡(jiǎn)單,路由和視圖就寫在一起了。

1.路由和視圖部分

from django.conf.urls import url
from django.shortcuts import render


def p1(request):
 return render(request, 'p1.html')

def p2(request):
 if request.method == 'GET':
  return render(request, 'p2.html')

 elif request.method == 'POST':
  city = request.POST.get('city')
  print('執(zhí)行數(shù)據(jù)保存操作...')
  return render(request, 'popup.html',{'city':city})

urlpatterns = [
 url(r'^p1.html/', p1),
 url(r'^p2.html/', p2),
]

2.訪問(wèn)視圖p1,返回頁(yè)面p1.html:

<head>
 <meta charset="UTF-8">
 <title>p1頁(yè)面</title>
</head>

<body>
<h2>p1頁(yè)面</h2>

<select id="cityChoose">
 <option>上海</option>
 <option>北京</option>
</select>
<input type="button" value="添加" onclick="popupFunc();"/>

<script>
 popupFunc = function () {
  window.open('/p2.html/', 'p2', "status=1, height:300, width:300, toolbar=0, resizeable=1")
  //open(url, name, 窗口參數(shù)),注意name不能重名
 };

 callback = function (city) {
  var opt = document.createElement('option');
  opt.innerText = city;
  opt.setAttribute('selected', 'selected');
  var selEle = document.getElementById('cityChoose');
  selEle.appendChild(opt);
 }
</script>
</body>

說(shuō)明:

1、點(diǎn)擊添加按鈕,執(zhí)行popupFunc:訪問(wèn)'/p2.html/',并在新窗口中打開(kāi)頁(yè)面p2.html
2、定義回調(diào)函數(shù)callback:它接收一個(gè)參數(shù)city,將其動(dòng)態(tài)添加到下拉選項(xiàng)中并設(shè)置為選中狀態(tài)。

3.彈出窗口中顯示p2.html如下:

<head>
 <meta charset="UTF-8">
 <title>p2頁(yè)面</title>
</head>
<body>
<h2>p2頁(yè)面</h2>

<form method="post">
 {% csrf_token %}
 <input type="text" name="city">
 <input type="submit" value="提交">
</form>
</body>


說(shuō)明:這里沒(méi)有指定form表單的action=url參數(shù),用戶輸入數(shù)據(jù)后,默認(rèn)提交到當(dāng)前地址,即'/p2.html/',提交到視圖p2

4.視圖p2收到提交的數(shù)據(jù)后,傳入模板并返回頁(yè)面popup.html:

<head>
 <meta charset="UTF-8">
 <title>正在返回</title>
</head>
<body>
<script>

 (function (city) {
  window.opener.callback(city);
  window.close();
 })("{{ city }}")

</script>
</body>


說(shuō)明:

  • 這里定義了JS自執(zhí)行函數(shù):它調(diào)用打開(kāi)彈出窗口的窗口中的回調(diào)函數(shù)(即P1中的callback),并把用戶輸入數(shù)據(jù)作為參數(shù)傳入;關(guān)閉自身。
  • 如果p2視圖返回錯(cuò)誤信息,也可以在popup.html中作顯示(略)。
  • 自執(zhí)行函數(shù)可以參考 JavaScript 自執(zhí)行函數(shù)和 jQuery擴(kuò)展方法

效果圖:

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論