常用js

//點(diǎn)擊城市輸入框顯示推薦城市,點(diǎn)擊其他區(qū)域推薦城市消失
$(".input_div_city").on("click", function(e){
    $(".input_div_select").show();

    $(document).one("click", function(){
        $(".input_div_select").hide();
    });

    e.stopPropagation();
});

/**
 * Created by DEV003 on 2019/3/5.
 */
//格式化表單數(shù)據(jù)
$.fn.serializeObject = function() {
    var o = {};
    var a = this.serializeArray();
    $.each(a, function() {
        if (o[this.name] !== undefined) {
            if (!o[this.name].push) {
                o[this.name] = [o[this.name]];
            }
            o[this.name].push(this.value || '');
        } else {
            o[this.name] = this.value || '';
        }
    });
    return o;
};

//模擬form表單請(qǐng)求
function httpPost(URL, PARAMS) {
     var temp = document.createElement("form");
     temp.action = URL;
     temp.method = "post";
     temp.style.display = "none";

     for (var x in PARAMS) {
     var opt = document.createElement("textarea");
     opt.name = x;
     opt.value = PARAMS[x];
     temp.appendChild(opt);
     }

     document.body.appendChild(temp);
     temp.submit();

     return temp;
 }

//時(shí)間戳轉(zhuǎn)換為日期
function timestampToTime(timestamp) {
    var date = new Date(timestamp);//時(shí)間戳為10位需*1000,時(shí)間戳為13位的話不需乘1000
    Y = date.getFullYear() + '-';
    M = (date.getMonth()+1 < 10 ? '0'+(date.getMonth()+1) : date.getMonth()+1) + '-';
    //D = date.getDate() + ' ';
    D = (date.getDate() < 10 ? '0'+date.getDate() : date.getDate())+' ';
    h = date.getHours() + ':';
    m = (date.getMinutes() < 10 ? '0'+(date.getMinutes()) : date.getMinutes());
    s = (date.getSeconds() < 10 ? '0'+(date.getSeconds()) : date.getSeconds());
    return Y+M+D+h+m;
}


//獲取checkbox多選框的值
function getCheckbox(dom) {
    var str = '';
    dom.each(function() {
        if ($(this).is(":checked")) {
            var val = $(this).val();
            str += val +',';
        }
    });
    if(str){
        str = str.substr(0,str.length-1,str);
    }
    return str;
}

//獲取當(dāng)前日期
function GetDateStr(AddDayCount) {
    var dd = new Date();
    dd.setDate(dd.getDate()+AddDayCount);//獲取AddDayCount天后的日期
    var y = dd.getFullYear();
    var m = dd.getMonth()+1;//獲取當(dāng)前月份的日期
    var d = dd.getDate();

    if(m<10) m = '0'+m;
    if(d<10) d = '0'+d;

    return y+"-"+m+"-"+d;
}
/*document.write("前天:"+GetDateStr(-2));
 document.write("<br />昨天:"+GetDateStr(-1));
 document.write("<br />今天:"+GetDateStr(0));
 document.write("<br />明天:"+GetDateStr(1));
 document.write("<br />后天:"+GetDateStr(2));
 document.write("<br />大后天:"+GetDateStr(3));*/


  //功能:計(jì)算兩個(gè)時(shí)間戳之間相差的日時(shí)分秒
    //$begin_time  開(kāi)始時(shí)間戳
    //$end_time 結(jié)束時(shí)間戳
    static function time_span($begin_time,$end_time)
    {
        if($begin_time < $end_time){
            $starttime = $begin_time;
            $endtime = $end_time;
        }else{
            $starttime = $end_time;
            $endtime = $begin_time;
        }
        //計(jì)算天數(shù)
        $timediff = $endtime-$starttime;
        $days = intval($timediff/86400);
        //計(jì)算小時(shí)數(shù)
        $remain = $timediff%86400;
        $hours = intval($remain/3600);
        //計(jì)算分鐘數(shù)
        $remain = $remain%3600;
        $mins = intval($remain/60);
        //計(jì)算秒數(shù)
        $secs = $remain%60;
        $res = array("day" => $days,"hour" => $hours,"min" => $mins,"sec" => $secs);
        return $res;
    }


//獲取篩選條件
    function getPostData() {
         var area_dom = $("input[name='area']");
         var brand_dom = $("input[name='brand']");
         var scenic_dom = $("input[name='scenic']");
         var facilityies_dom = $("input[name='facilityies']");
         var star_dom = $("input[name='star']");
         var price_range = $("input[name='price_range']").is(":checked");
         if(price_range){
            var price = $("input[name='price_range']:checked").val();
            var regular1  = /<+/;
            var regular2  = />+/;
            if(regular1.exec(price)){
                var minprice = '';
                var maxprice = price.replace('<', '');

            }else if(regular2.exec(price)){
                var minprice = price.replace('>', '');
                var maxprice = '';

            }else{
                price = price.split('-');
                var minprice = price[0];
                var maxprice = price[1];
            }
         }else{
            var minprice = $("input[name='minprice']").val();
            var maxprice = $("input[name='maxprice']").val();
         }

        var json = {
             area:getCheckbox(area_dom),
             brand:getCheckbox(brand_dom),
             scenic:getCheckbox(scenic_dom),
             facilityies:getCheckbox(facilityies_dom),
             star:getCheckbox(star_dom),
             minprice:minprice,
             maxprice:maxprice
         }
         return json;
    }
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容