应该一:
无刷新联动效果get方式:
public ActionResult MaterialByClass(string l) { var materials = _db.Products.Where(m => m.CodeClass == l).ToList().Select(a => new SelectListItem() { Text = a.Material.Name != null ? a.Material.Name : "[NO Date}", Value = a.CodeMaterial }); return Json(materials, JsonRequestBehavior.AllowGet); }
$("#@orgFieldId").change(function () { var idProd = $(this).val(); $.getJSON("/JsonService/GetAccounts", { orgId: idProd }, function (myData) { var select = $("#@fieldId"); select.empty(); $.each(myData, function (index, itemData) { select.append($('<option/>', { value: itemData.Value, text: itemData.Text })); }); }); }); $(document).ready(function () { $("#@orgFieldId").change(); });
上面的代码也可以使用如下方式(两个是等效的,只不过上面的没有下面的灵活性高,特别是设置默认值方面:
$(function () { $("#l").change(function () { var url = "/Home/MaterialByClass"; var id_cz = $("#l").val(); $.getJSON(url, { l: id_cz }, function (myData) { var items = ""; $.each(myData, function (i, cz) { items += "<option value='" + cz.value + "'>" + cz.Text + "</option>"; }); $("#c").html(items); }); }); }); $(function () { $("#l").change(); });
应用二:
无刷新Post应用形式
[HttpPost] public ActionResult AddGroupAccount(string useraccount) { var result = new GroupViewModel { Id = addid, SubUserAccount = useraccount, Company=company, Statu="<span style='color:Red'>等待对方确认</span>", Message = message }; return Json(result); }
$(function () { $(".addbtn").click(function () { var put_sub_user = $("#put_sub_useraccount").val(); if (put_sub_user != '') { $.post("/User/Index/AddGroupAccount", { "useraccount": put_sub_user }, function (data) { if (data.Message == "添加成功!") { $("#userlist").prepend("<tr id=row-" + data.Id + "><td>" + data.SubUserAccount + "-" + data.Company + " [" + data.Statu + "]</td><td width='57'><a href='#' class='RemoveLink' data-id=" + data.Id + ">删除</a></td></tr>"); $("#message").text(data.Message); } else $("#message").text(data.Message); }); } else $("#message").text("出错!输入框不能为空!"); });
如果是传递多个参数可以这样用:
var dataid = $("#curr_id").val(); var datapcs = $("#PCS").val(); $.post("/ShoppingCart/AddToCart", { id: dataid,pcs:datapcs}, function (data) { if (data.Message = "ok") alert(data.Message); else alert("Error"); });
学习交流群:364976091