Jquery代码片断

来自tomtalk
跳转至: 导航搜索

jquery file upload

html
<!DOCTYPE HTML>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title>jQuery File Upload Demo</title>
    <style>
        .bar {
            height: 18px;
            background: green;
        }
    </style>
</head>
<body>
 
<div class="container">
    <input id="fileupload" type="file" name="files[]" data-url="/js/jQuery-File-Upload/server/php/" multiple>
 
 
    <div id="progress">
        <div class="bar" style="width: 0%;"></div>
    </div>
</div>
 
<script src="/assets/js/jquery-1.8.2.min.js"></script>
<script src="js/vendor/jquery.ui.widget.js"></script>
<script src="js/jquery.fileupload.js"></script>
<script src="js/main.js"></script>
</body>
</html>
js
$(function () {
 
    $('#fileupload').fileupload({
        dataType: 'json',
        add: function (e, data) {
            data.submit();
        },
        done: function (e, data) {
            $.each(data.result.files, function (index, file) {
                $('<p/>').text(file.name).appendTo(document.body);
            });
        },
        formData: [
            {
                name: '_http_accept',
                value: 'application/javascript'
            }
        ],
        progressall: function (e, data) {
            var progress = parseInt(data.loaded / data.total * 100, 10);
            $('#progress .bar').css('width', progress + '%');
        }
    });
});


购物车,全选复选框

$("input[name=cart_select_all]").on('click', function () {
    var btn = $("input[name=cart_select_all]");
    var items = $("tbody input[type=checkbox]");
 
    if ($(this).attr("checked")) {
        btn.attr('checked', true);
        items.attr('checked', true);
    } else {
        btn.attr('checked', false);
        items.attr('checked', false);
    }
});
$("input:radio[name=is_relative_had]").change(function () {
    if ($(this).val() === '1') {
        $('div.relative').show();
    } else {
        $('div.relative').hide();
    }
});
 
//选择开票,输入开票数据
$('input[name=is_invoice]').click(function (obj) {
    var chk = $('input[name=is_invoice]:checked');
 
    if (chk.length == 0) {
        $('#invoice_item').hide();
    } else {
        $('#invoice_item').show();
    }
});

Jquery表单获取选中的值

//获取select被选中项的文本
var item = $("select[@name=items] option[@selected]").text();
 
//select下拉框的第二个元素为当前选中值
$('#select_id')[0].selectedIndex = 1;
 
//radio单选组的第二个元素为当前选中值
$('input[@name=items]').get(1).checked = true;
 
//根据值设置radio选中状态
$("input[name=test][value=34]").attr("checked",true);     //value=34的radio被选中
$("input[id=testid][value=34]").attr("checked",true);     //value=34的radio被选中
$('input:radio[name=sex]')[1].checked = true;  //上面两种方法不能用,就用这种方法
 
//文本框
$("#txt").attr("value");
$("#txt").val();
 
//下拉框select:
$('#sel').val();
 
//checkbox:
$("#chk1").attr("checked",'');            //不打勾
$("#chk2").attr("checked",true);          // 打勾
if($("#chk1").attr('checked')==undefined) //判断是否已经打勾
 
//radio:
$("input[@type=radio]").attr("checked",'2'); //设置value=2的项目为当前选中项
 
//下拉框select
//设置 value=-sel3的项目为当前选中项
$("#sel").attr("value",'-sel3');
 
//添加下拉框的 option
$("<optionvalue='1'>1111</option>& lt;optionvalue='2'>2222</option>").appendTo("#sel")
$("#sel").empty()//清空下拉框
 
//jquery获取复选框值
function jqchk() {
    var chk_value =[];  
    $('input[name="test"]:checked').each(function(){  
        chk_value.push($(this).val());  
    });  
    alert(chk_value.length==0 ?'你还没有选择任何内容!':chk_value);  
}  
 
$("#text_id").val();                          //AREATEXT的值
$("input[name=payment]:checked").val()        //获取单选按钮的值
 
//jquery表格奇数行加底纹。
$('#order_list tr:nth-child(even)').addClass('table_blue_line');

代码片段

$("#copied-mark").delay('slow').fadeOut();
 
$('#platform-select').change(function (obj) {
    var val = $(this).children('option:selected').val();
    console.log(val);
});
 
$("p").css("color");  //获取p元素的样式颜色
 
jQuery(function($){
    $(function(){
        <!--{if $_FANWE['page']>1}-->
        window.location.hash="#publish_topic";
        <!--{/if}--> 
    })
});
 
//基本的post
$("input").keyup(function(){
    txt=$("input").val();
 
    $.post("demo_ajax_gethint.asp",{suggest:txt},function(result){
        $("span").html(result);
    });
});
 
//处理错误
$.post("http://www.tomtalk.net/getList.php", {start: 0, limit: 1}, function (result) {
    _.each(result, function (row) {
        content_view.add(row);
    });
    content_view.render();
}, 'json')
.error(function () {
    $('#content').html('数据读取出错!');
});
 
//处理超时
$.ajax({
    url: "http://www.tomtalk.net/getList.php",
    type: "POST",
    data: {start: 0, limit: 1},
    timeout: 3000,
    dataType: "json",
    success: function (result) {
        //console.log(result);
        _.each(result, function (row) {
            content_view.add(row);
        });
        content_view.render();
    },
    error: function () {
        $('#content').html('数据读取出错!');
    }
});