1.資料庫截取字元串:toFixed():四捨五入substring(cp_introduce,0,11) cp_introduce前臺截取: field: 'an_content', title: '問題內容', formatter: function (value) { if (value.le ...
1.資料庫截取字元串:
toFixed():四捨五入
substring(cp_introduce,0,11) cp_introduce
前臺截取:
field: 'an_content',
title: '問題內容',
formatter: function (value) {
if (value.length > 25) {
return value.substring(0, 25) + "…";
} else {
return value;
}
}
convert(nvarchar(10),field,120)
2.覆選框
設置
<input type="checkbox" value="1" name="sProblem">check1
<input type="checkbox" value="2" name="sProblem">check2
<input type="checkbox" value="3" name="sProblem">check3
<input type="checkbox" value="4" name="sProblem">check4
取值
獲取選中的checkbox的val,併合成為一個字元串以逗號隔開。
function getTheCheckBoxValue(){
var test = $("input[name='sProblem']:checked");
var checkBoxValue = "";
test.each(function(){
checkBoxValue1 += $(this).val()+",";
})
checkBoxValue = checkBoxValue.substring(0,checkBoxValue.length-1);
3.設置文本框只讀的2種方式(不要用到style)
disabled:disabled
readonly:readonly
4.
selectListItem綁定數據
固定的數據
List<SelectListItem> wlist = new List<SelectListItem>();
wlist.Add(new SelectListItem { Text = "經驗不限", Value = "0" });
wlist.Add(new SelectListItem { Text = "無經驗", Value = "1" });
wlist.Add(new SelectListItem { Text = "1年以下", Value = "2" });
wlist.Add(new SelectListItem { Text = "1-3年", Value = "3" });
wlist.Add(new SelectListItem { Text = "3- 5年", Value = "4" });
wlist.Add(new SelectListItem { Text = "5- 10年", Value = "5" });
wlist.Add(new SelectListItem { Text = "10年以上", Value = "6" });
ViewBag.wlist = wlist;
獲取資料庫的數據:
ViewBag.wlist = 數據源;
前臺獲取
<select id="jp_Education" name="jp_Education">
<option value="-1">請選擇學歷</option>
@foreach (var item in ViewBag.elist)
{
<option value="@item.Value">@item.Text</option>
}
</select>
5.省市聯動
引用的js文件:
<script src="~/Content/assets/data.js"></script>
<script src="~/Content/assets/jquery-1.12.4.js"></script>
<script src="~/Content/assets/province.js"></script>
html代碼:
<div class="form-group layui-form-item">
<div class="layui-form-item">
<label class="layui-form-label">工作地區</label>
<div class="layui-input-inline">
<select name="provid" id="provid" lay-filter="provid">
<option value="">請選擇省</option>
</select>
</div>
<div class="layui-input-inline">
<select name="cityid" id="cityid" lay-filter="cityid">
<option value="">請選擇市</option>
</select>
</div>
<div class="layui-input-inline">
<select name="areaid" id="areaid" lay-filter="areaid">
<option value="">請選擇縣/區</option>
</select>
</div>
</div>
</div>
js代碼:
獲取地區
var jp_address1 = $("#provid option:selected").text();
var jp_address2 = $("#cityid option:selected").text();
var jp_address3 = $("#areaid option:selected").text();
var jp_address = jp_address1 + " " + jp_address2 + " " + jp_address3;
6.嵌套的三元運算符
示例:
審核狀態:
<span>
@(ViewBag.company.cp_type == 0 ? "待審核" :
ViewBag.company.cp_type == 1 ? "通過" :
ViewBag.company.cp_type == 2 ? "未通過" : "")
</span>
7.百度富文本編輯器
引用的js文件:
<script src="~/Content/ueditor/ueditor.config.js"></script>
<script src="~/Content/ueditor/ueditor.all.min.js"></script>
html代碼:
<div class="form-group layui-form-item">
<label class="layui-form-label">任職要求</label>
<div class="layui-input-block">
<script id="content" type="text/plain" style="width:100%;">
</script>
<textarea id="jp_description" name="jp_description" style="display:none;" ></textarea>
</div>
</div>
js代碼:
初始化富文本編輯器:
$(function () {
//百度富文本初始化
UE.getEditor('content',{
initialFrameHeight: 200
})
});
獲取富文本輸入的內容:
var jp_description = UE.getEditor('content').getContentTxt();
8.layui頁面渲染:
layui.use('form', function (){
var form = layui.form;
//但是,如果你的HTML是動態生成的,自動渲染就會失效
//因此你需要在相應的地方,執行下述方法來手動渲染,跟這類似的還有 element.init();
form.render();
});
9.圖片上傳(上傳不同的圖片使用不同的id)
引用的js文件:
<script src="~/Content/js/ajaxfileupload.js" type="text/javascript"></script>
html代碼:
<div id="attachs2" class="form-group layui-form-item">
<input type="hidden" class="form-control" id="attach2" name="attach2" v-model="JSON.stringify(items2)" value="">
<label class="layui-form-label">公司logo</label>
<div class="layui-input-block">
<div class="input-group">
<div class="input-group-btn">
<a onclick="$('#file2').click()" class="btn btn-link"><i class="glyphicon glyphicon-plus"></i>上傳公司logo</a>
<input id="file2" name="file2" type="file" multiple="multiple" onchange="addAttach2('圖片上傳到的文件夾')" class="hide" />
</div>
</div>
<div class="input-group">
<span v-for="(item,index) in items2">
<a v-bind:href="[item.path]" target="_blank"><img v-bind:src="[item.path]" style="width:50px;" target="_blank" />{{item.name}}</a>
<a class="btn btn-link" v-on:click="remove(index)">刪除</a>
</span>
</div>
</div>
</div>
js代碼:
//上傳公司logo圖片
var attachs2 = new Vue({
el: '#attachs2',
data: { items2: [] },
created: function (){
if ($('#attach2').val() != '') { this.items2 = JSON.parse($('#attach2').val()); }
},
methods:{
remove: function (index){
var path = this.items2[index]['path'];
$.ajax({
url: '/Upload/Delete',
data: { 'path': path },
success: function (data){; }
})
this.items2.splice(index, 1);
}
}
});
attachs2.$watch("items2", function (){
showattachs.items2 = attachs2.items2;
})
var showattachs = new Vue({
el: "#showattachs",
data: { items2: [] },
created: function (){
this.items = attachs2.items;
}
})
function addAttach2(path){
$.ajaxFileUpload({
url: '/Upload/FileList/',
data: { 'path': path },
secureuri: false,
fileElementId: 'file2',
dataType: 'HTML',
success: function (data) {
attachs2.items2 = attachs2.items2.concat(JSON.parse(data));
}
})
}
上傳圖片的專用控制器:
using System;
using System.Text;
using System.Collections.Generic;
using System.IO;
using System.Web;
using System.Web.Mvc;
namespace ApiProject.Controllers
{
public class UploadController : Controller
{
public ActionResult Index()
{
return View();
}
public Dictionary<string,object> UploadFile(string path)
{
Dictionary<string, object> data = new Dictionary<string, object>();
string userPath = "/Files/Job/";
if (!Directory.Exists(Server.MapPath(userPath))) { Directory.CreateDirectory(Server.MapPath(userPath)); }
string dir = userPath + "/" + path;
if (!Directory.Exists(Server.MapPath(dir))) { Directory.CreateDirectory(Server.MapPath(dir)); }
HttpFileCollection hfc = System.Web.HttpContext.Current.Request.Files;
if (hfc.Count > 0)
{
string fileName = Path.GetFileNameWithoutExtension(hfc[0].FileName);
string ext = Path.GetExtension(hfc[0].FileName);
string filePath = dir + "/" + DateTime.Now.ToString("yyyyMMddHHmmssfff") + ext;
hfc[0].SaveAs(Server.MapPath(filePath));
data["name"] = fileName;
data["path"] = filePath;
return data;
}
else
{
return null;
}
}
public ActionResult FileList(string path)
{
string userPath = "/Files/Job/";//圖片保存的路徑
if (!Directory.Exists(Server.MapPath(userPath))) { Directory.CreateDirectory(Server.MapPath(userPath)); }
string dir = userPath + "/" + path;
if (!Directory.Exists(Server.MapPath(dir))) { Directory.CreateDirectory(Server.MapPath(dir)); }
HttpFileCollection files = System.Web.HttpContext.Current.Request.Files;
StringBuilder sb = new StringBuilder();
sb.Append("[");
for (int i = 0; i < files.Count; i++)
{
string fileName = Path.GetFileNameWithoutExtension(files[i].FileName);
string ext = Path.GetExtension(files[i].FileName);
string filePath = dir + "/" + DateTime.Now.ToString("yyyyMMddHHmmssfff")+i+ ext;
files[i].SaveAs(Server.MapPath(filePath));
sb.Append("{\"name\":\"" + fileName + "\",\"path\":\"" + filePath + "\" }");
if (i < files.Count-1) { sb.Append(","); }
}
sb.Append("]");
return Content(sb.ToString());
}
public ActionResult File(string path)
{
Dictionary<string,object> data = UploadFile(path);
if (data != null)
{
return Content("{\"name\":\"" + data["name"].ToString() + "\",\"path\":\"" + data["path"].ToString() + "\"}");
}
else
{
return Content("");
}
}
public void Delete(string path)
{
System.IO.File.Delete(Server.MapPath(path));
}
}
}
10.
點擊button自動提交表單原因及解決方案
原因
出現上述的問題主要是button標簽的type屬性惹的禍,button的type屬性值有三個分別為button、submit、reset。當我們在利用button標簽寫一個按鈕且沒有指定其type屬性時,IE7以下版本(具體是IE7以下還是IE5以下給忘了)會預設指定為button,其他會被預設指定為submit。當按鈕的type屬性被指定為submit的時候,點擊它會提交表單。
解決
當需在form標簽中放置一個button的時候,如果這個按鈕不是做提交表單的,切記一定要設置其type為button。
11.z-index