项目中常用的JS操作技巧
2015-08-05 16:16
676 查看
1.<a>标签-超链接中confirm方法使用介绍
<a href="a.html" onclick="if(confirm('确定删除?')==false)return false;">删除</a>
2.<select>下拉框三级联动效果
1).html代码
<select name="select_element" id="firstServiceType"></select>
<select name="select_element" id="secondServiceType"></select>
<select name="select_element" id="thirdServiceType"></select>
2).JS代码
<script type="text/javascript">
$(document).ready(function () {
$("#code").val();
GetFirstType();
$("#firstServiceType").change(function () { $("#Type").val($(this).val()); GetSecondType() });
$("#secondServiceType").change(function () { $("#Type").val($(this).val()); GetThirdType() });
$("#thirdServiceType").change(function () { $("#Type").val($(this).val()); });
});
function GetFirstType() {
$("#firstServiceType").empty();
$("#firstServiceType").append('<option>请选择</option>');
$.getJSON("/ServiceType/GetFirstType", function (data) {
$.each(data, function (i, item) {
$("<option></option>")
.val(item["Code"])
.text(item["AbbrName"])
.appendTo($("#firstServiceType"));
});
GetSecondType();
});
}
function GetSecondType() {
$("#secondServiceType").empty();
$("#secondServiceType").append('<option value="0">请选择</option>');
var url = "/ServiceType/GetSecondType/?code=" + $("#firstServiceType").val();
$.getJSON(url, function (data) {
$.each(data, function (i, item) {
$("<option></option>")
.val(item["Code"])
.text(item["AbbrName"])
.appendTo($("#secondServiceType"));
});
GetThirdType();
});
}
function GetThirdType() {
$("#thirdServiceType").empty();
$("#thirdServiceType").append('<option value="0">请选择</option>');
var url = "/ServiceType/GetThirdType/?code=" + $("#secondServiceType").val();
$.getJSON(url, function (data) {
$.each(data, function (i, item) {
$("<option></option>")
.val(item["Code"])
.text(item["AbbrName"])
.appendTo($("#thirdServiceType"));
});
});
}
</script>
3.上传图片时预览功能
1).htm代码
<img id="img" style="width:100px; height:100px;" src=""/>
<input type="file" name="pic" id="file" />
2).JS代码
<script type="text/javascript">
$(document).ready(function () {
$("#file").bind("change", function () {
var f = document.getElementById('file').files[0];
var src = window.URL.createObjectURL(f);
$("#img").attr("src", src);
})
});
</script>
<a href="a.html" onclick="if(confirm('确定删除?')==false)return false;">删除</a>
2.<select>下拉框三级联动效果
1).html代码
<select name="select_element" id="firstServiceType"></select>
<select name="select_element" id="secondServiceType"></select>
<select name="select_element" id="thirdServiceType"></select>
2).JS代码
<script type="text/javascript">
$(document).ready(function () {
$("#code").val();
GetFirstType();
$("#firstServiceType").change(function () { $("#Type").val($(this).val()); GetSecondType() });
$("#secondServiceType").change(function () { $("#Type").val($(this).val()); GetThirdType() });
$("#thirdServiceType").change(function () { $("#Type").val($(this).val()); });
});
function GetFirstType() {
$("#firstServiceType").empty();
$("#firstServiceType").append('<option>请选择</option>');
$.getJSON("/ServiceType/GetFirstType", function (data) {
$.each(data, function (i, item) {
$("<option></option>")
.val(item["Code"])
.text(item["AbbrName"])
.appendTo($("#firstServiceType"));
});
GetSecondType();
});
}
function GetSecondType() {
$("#secondServiceType").empty();
$("#secondServiceType").append('<option value="0">请选择</option>');
var url = "/ServiceType/GetSecondType/?code=" + $("#firstServiceType").val();
$.getJSON(url, function (data) {
$.each(data, function (i, item) {
$("<option></option>")
.val(item["Code"])
.text(item["AbbrName"])
.appendTo($("#secondServiceType"));
});
GetThirdType();
});
}
function GetThirdType() {
$("#thirdServiceType").empty();
$("#thirdServiceType").append('<option value="0">请选择</option>');
var url = "/ServiceType/GetThirdType/?code=" + $("#secondServiceType").val();
$.getJSON(url, function (data) {
$.each(data, function (i, item) {
$("<option></option>")
.val(item["Code"])
.text(item["AbbrName"])
.appendTo($("#thirdServiceType"));
});
});
}
</script>
3.上传图片时预览功能
1).htm代码
<img id="img" style="width:100px; height:100px;" src=""/>
<input type="file" name="pic" id="file" />
2).JS代码
<script type="text/javascript">
$(document).ready(function () {
$("#file").bind("change", function () {
var f = document.getElementById('file').files[0];
var src = window.URL.createObjectURL(f);
$("#img").attr("src", src);
})
});
</script>
相关文章推荐
- js格式文件中获取JSP变量的方法
- 使用sina js 根据文件名作股票代码 看股票
- js 利用闭包循环给li绑定参数不同的点击事件
- JS简单实现拖拽可视化构图
- javascript dom与字符串相互转换
- 关于jsp页面中中文乱码问题的解决方法
- JS中函数
- 【JavaScript算法】javascript常用算法
- JSON处理
- js 按键事件
- Jstat使用详解
- xStream完美转换XML、JSON
- ExtJs 请求服务器-自定义默认参数
- JS实现点击上移下移LI行数据的方法
- javascript的一些小知识1
- JSP中文乱码问题终极解决方案
- JS模拟键盘打字效果的方法
- 关于JavaScript与HTML的交互事件
- js点击列表文字对应该行显示背景颜色的实现代码
- javascript基本特点,组成和应用