dropdownlist本身不支持图像列表,那么我们利用jquery来实现下拉列表的模拟。
成都创新互联-专业网站定制、快速模板网站建设、高性价比郫都网站开发、企业建站全套包干低至880元,成熟完善的模板库,直接使用。一站式郫都网站制作公司更省心,省钱,快速模板网站建设找我们,业务覆盖郫都地区。费用合理售后完善,十多年实体公司更值得信赖。
[[3393]]
用到的表
如下图,因为不是重点,不再介绍,只要注意一点,company.id为自增int,注意属性窗口中auto Generated Value为true,Auto-Sync为OnInsert,这样linq才可没错
- [AcceptVerbs(HttpVerbs.Post)]
- publicActionResult CreateCompany(string name, string brief)
- {
- string strResult = "失败";
- ServiceDataContext db =newServiceDataContext();
- Company com =newCompany();
- com.Name = name;
- com.Brief = brief;
- try
- {
- db.Companies.InsertOnSubmit(com);
- db.SubmitChanges();
- strResult = "成功";
- }
- catch
- {
- strResult = "失败,名称重复?";
- }
- returnJson(strResult);
- }
- id="imgDefault" src='<%=ViewData["selectedUrl"]%>' class="imgCss" alt=""/>
- id="imgArrow" src="<%=ViewData["arrowUrl"]%>" class="DropdownCss" alt=""/>
id="divList" class="scrolldivHidden">- <%=ViewData["imageTable"]%> div>
- <%=Html.Hidden("head") %>
- td>
这里selectedUrl为选择的图像,有默认值,arrowUrl为下拉图标,ViewData["imageTable"]是下拉列表框
这里用到的css
- .Visible
- {
- visibility: visible;
- display: block;
- }
- .Hidden
- {
- visibility: hidden;
- display: none;
- }
- .imgCss
- {
- cursor: hand;
- width: 18px;
- height: 18px;
- border: 1px solid #c3c3c3;
- }
- .DropdownCss
- {
- cursor: hand;
- width: 9px;
- height: 9px;
- vertical-align: top;
- }
- .scrolldivHidden
- {
- position:absolute;
- width: 36px;
- height: 50px;
- z-index: 1;
- left: 0px;
- top: 0x;
- overflow-y: auto;
- overflow-x:hidden;
- display:none;
- }
- .scrolldivVisible
- {
- position:absolute;
- width: 36px;
- height: 50px;
- z-index: 1;
- left: 0px;
- top: 0x;
- overflow-y: auto;
- overflow-x:hidden;
- display:block;
- }
主要我们用到jquery来实现选择等功能
这里用到jquery一个选择框的插件
- /*
- * Manipulation for HTML SELECT with jQuery
- * Created by Baldwin (http://www.dnnsun.com)
- * version: 1.0 (02/03/2009)
- * @requires jQuery v1.2 or later
- */
- ; (function($) {
- /* Clear all options */
- $.fn.clearSelect =function() {
- returnthis.each(function() {
- if(this.tagName == 'SELECT')this.options.length = 0;
- });
- }
- /* Fill the options with the object array: [{'Text':'Hello','Value':'1'}]*/
- $.fn.fillSelect =function(data) {
- returnthis.clearSelect().each(function() {
- if(this.tagName == 'SELECT') {
- vardropdownList =this;
- if(data && data.length > 0) {
- $.each(data,function(index, optionData) {
- varoption =newOption(optionData.Text, optionData.Value);
- if($.browser.msie)
- dropdownList.add(option);
- else
- dropdownList.add(option,null);
- });
- dropdownList.disabled =false;
- }
- else
- dropdownList.disabled =true;
- }
- });
- }
- /* loading when applying Ajax fillSelect */
- $.fn.loadSelect =function(loadText) {
- vardata = [{ 'Text': loadText, 'Value': ''}];
- this.fillSelect(data);
- }
- /* selected the target option with value */
- $.fn.selected =function(value) {
- returnthis.each(function() {
- if(this.tagName == 'SELECT') {
- varoptions =this.options;
- if(options.length > 0) {
- $.each(options,function(index, optionData) {
- // once match then exist loop
- if(optionData.value == value) {
- optionData.selected =true;
- returnfalse;
- }
- });
- }
- }
- });
- }
- /* TODO:selected the target option with text */
- $.fn.selectedText =function(text) {
- returnthis.each(function() {
- if(this.tagName == 'SELECT') {
- varoptions =this.options;
- if(options.length > 0) {
- $.each(options,function(index, optionData) {
- // once match then exist loop
- if(optionData.text == text) {
- optionData.selected =true;
- returnfalse;
- }
- });
- }
- }
- });
- }
- /* returns the selected value */
- $.fn.getSelected =function() {
- return$(this).val();
- }
- /* return the text of selected option */
- $.fn.getSelectedText =function() {
- return$(this).children("[@selected]").text();
- }
- })(jQuery);
- functiongetDepartment(data) { $("#department").fillSelect(data); }
- $("#company").change(function() {
- $.getJSON("GetDepartment?company=" + $("#company").val(),null, getDepartment);
- });
实现了联动框
下面的代码实现了下拉和选择功能
- $("#imgArrow,#imgDefault").toggle(function() {
- $("#divList").css("left", $("#imgDefault").css("left"));
- $("#divList").slideToggle("fast");
- },function() {
- $("#divList").css("left", $("#imgDefault").css("left"));
- $("#divList").slideToggle("fast");
- });
- $("#head").val($("#imgDefault").attr("src"));
- $("img:.imgList").click(function() {
- $("#imgDefault").attr("src", $(this).attr("src"));
- $("#divList").slideToggle("fast");
- $("#head").val($("#imgDefault").attr("src"));
- }).hover(function() {
- $(this).removeClass().addClass("alpha");
- }, (function() {
- $(this).removeClass();
- }));
- $("body").click(function() {
- $("#divList").slideUp("fast");
- $("#imgDefault").hover(function() {
- $(this).addClass("alpha");
- },function() {
- $(this).removeClass();
- });
- });
还有我们用到的action
- publicActionResult CreateService()
- {
- ViewData["company"] = GetCompanyList();
- List blank=newList ();
- blank.Add(GetBlankDepartment());
- ViewData["department"] = blank.ToArray();
- ViewData["head"] = GetHeadList();
- ViewData["selectedUrl"] = Url.Content(Head_Folder + "Head/head0.gif");
- ViewData["arrowUrl"] = Url.Content(Head_Folder + "/dropdown.gif");
- ViewData["imageTable"] = GetImageTable();
- returnView();
- }
- [AcceptVerbs(HttpVerbs.Get)]
- publicActionResult GetDepartment(int? company)
- {
- SelectListItem[] items=null;
- if(company!=null)
- items = GetDepartmentList((int)company);
- else
- {
- SelectListItem i=GetBlankDepartment();
- items=newSelectListItem[]{i};
- }
- returnJson(items);
- }
- privateSelectListItem[] GetCompanyList()
- {
- ServiceDataContext db =newServiceDataContext();
- varitems = from tindb.Companies
- selectnew
- {
- ID = t.ID,
- Name = t.Name,
- Brief = t.Brief
- };
- List s =newList ();
- SelectListItem b =newSelectListItem();
- b.Text = "--please select company--";
- b.Value = "";
- s.Add(b);
- foreach (variinitems)
- {
- SelectListItem item =newSelectListItem();
- item.Text = i.Name;
- item.Value = i.ID.ToString();
- s.Add(item);
- }
- returns.ToArray();
- }
- privateSelectListItem[] GetDepartmentList(intnCompanyId)
- {
- ServiceDataContext db =newServiceDataContext();
- varitems = from tindb.Departments
- where t.Company==nCompanyId
- selectnew
- {
- ID = t.ID,
- Name = t.Name,
- Brief = t.Brief
- };
- List s =newList ();
- SelectListItem b =newSelectListItem();
- b.Text = "--please select department--";
- b.Value = "";
- s.Add(b);
- foreach (variinitems)
- {
- SelectListItem item =newSelectListItem();
- item.Text = i.Name;
- item.Value = i.ID.ToString();
- s.Add(item);
- }
- returns.ToArray();
- }
- privatestring[] GetHeadList()
- {
- List head =newList ();
- string strServer=Url.Content("~/Content/images/Head/");
- string strUrlFolder = Server.MapPath(Url.Content(Head_Folder+"Head/"));
- string[] fileEntries = Directory.GetFiles(strUrlFolder);
- FileInfo fi=null;
- foreach (string fileinfileEntries)
- {
- fi =newFileInfo(file);
- if(fi.Name.ToLower().IndexOf("head")==0 && !fi.FullName.ToLower().Contains("_offline"))
- {
- head.Add(strServer+fi.Name);
- }
- }
- returnhead.ToArray();
- }
- privatestring GetImageTable()
- {
- System.Text.StringBuilder sbResult =newSystem.Text.StringBuilder();
- string[] arrImgs = GetHeadList();
- foreach (string strImageinarrImgs)
- {
- string strFile=strImage.Substring(strImage.LastIndexOf("/")+1);
- sbResult.AppendLine(string.Format("",
- "img_"+strFile.Remove(strFile.LastIndexOf(".")),
- Url.Content(strImage)
- ));
- }
- string strImg=sbResult.ToString();
- strImg=strImg.Remove(strImg.Length-5,5);
- returnstrImg;
- }
- }
注意紫色处,我们加了cssimgList来供jquery选择
- $("img:.imgList").click(function() {
- $("#imgDefault").attr("src", $(this).attr("src"));
- $("#divList").slideToggle("fast");
- $("#head").val($("#imgDefault").attr("src"));
- }).hover(function() {
- $(this).removeClass().addClass("alpha");
- }, (function() {
- $(this).removeClass();
- }));
好了,可以试试效果了。
名称栏目:利用img和div标签实现图像下拉列表的模拟
路径分享:http://www.shufengxianlan.com/qtweb/news42/301492.html网站建设、网络推广公司-创新互联,是专注品牌与效果的网站制作,网络营销seo公司;服务项目有等
声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联
猜你还喜欢下面的内容
- Linux输入中文:配置方法大全(linux中怎样输入汉字)
- 服务器启动很慢的原因?企业服务器慢有哪些原因
- 创新互联Python教程:python中next函数如何使用?
- Redis开启新的数据存储时代(redis读法)
- html如何给按钮加链接
- 技术移民必看:工程师应该如何获得美国签证?
- 为啥租用美国服务器搭建外贸网站
- Vue开发必须知道的36个技巧【近1W字】
- vivo手机出现很抱歉,手机出现异常,即将退出程序是怎么回事?(报错请给我看看处理一下问题)
- 创新互联linux教程:3.3命令行的通配符
- 叛逃半感染者据点多久刷新一次?(刷新网站地图是否影响服务器)
- 国外免费服务器(不需要加群就能登录的服务器)
- 深入理解:数据库实例的重要作用 (数据库实例的作用)
- PHP函数:error_reporting
- windows10打开不了exe?(windows10打开不了exe的程序)