例如要向伺服端要一串資料來當DropDownList的item
==================
.......View
<div id="selectArea"> <!-- 選擇相簿與相片 -->
<select id="FileImageAb">
</select>
<input type="button" onclick="getFileAlbum()" name="btnGet" value="取得" />
</div>
.......JQuery
//取得目前所有相簿
function getFileAlbum() {
$.getJSON('<%=Url.Content("~/AjaxInfo/GetFileImageAlbumAll")%>', null,
function (data) {
$("#FileImageAb").children().remove();
for (i = 0; i < data.length; i++) {
if(i == 0) { //將第一個設給隱藏欄位
$("#ClassInfoId").val(data[i].FileImageAlbumId);
}
$("#FileImageAb").append(
$("<option></option>").attr("value",data[i].FileImageAlbumId).text(data[i].FIAName)
);
}
});
}
......Controller
public ActionResult GetFileImageAlbumAll()
{
using (DBEntities db = new DBEntities())
{
var result = (from s in db.FileImageAlbum select new { s.FileImageAlbumId,s.FIAName }).ToList();
return Json(result,JsonRequestBehavior.AllowGet);
}
}
==========================================
如果想要依據某個隱藏欄位(id=ClassInfoId)決定目前select(id=selectClassInfoId)的選取項目:
$.getJSON('<%=Url.Content("~/AjaxInfo/GetClassInfoAll")%>', null,
function (data) {
$("#selectClassInfoId").children().remove();
for (i = 0; i < data.length; i++) {
$("#selectClassInfoId").append(
$("<option></option>").attr("value", data[i].ClassInfoId).text(data[i].Name)
);
$("#selectClassInfoId").val($("#ClassInfoId").val()); //將課程Id設定給下拉選單
}
});
標籤
ASP.NET MVC
(29)
Visual C#
(15)
JQuery
(10)
Plugins
(8)
JQuery Plugins
(6)
JavaScript
(6)
MySQL
(5)
CSS
(4)
LinQ
(4)
Mac OS
(4)
CentOS
(3)
Design Pattern
(3)
Entity Framework
(3)
IIS
(3)
Python
(3)
Windows
(3)
php
(3)
Docker
(2)
LAMP
(2)
SQL Server
(2)
WCF
(2)
.NET
(1)
.NET Core
(1)
AWS
(1)
Browser
(1)
GIS
(1)
IE
(1)
Internet Security
(1)
Linux
(1)
Platform
(1)
React
(1)
SEO
(1)
Testing
(1)
VMware
(1)
Windows 7
(1)
cookie
(1)
curl
(1)
laravel
(1)
phpBB
(1)
session
(1)
中古屋
(1)
透天
(1)
閒言閒語
(1)
面試
(1)
鳥松
(1)
沒有留言:
張貼留言