consultform.js 1.93 KB
/**
 *  我要咨询提交页面
 * @author: liangzhifeng<zhifeng.liang@yoho.cn>
 * @date: 2015/12/01
 */
var $ = require('jquery'),
    tip = require('../../plugin/tip'),
    loading = require('../../plugin/loading');

var $consultForm = $('.consult-form'),
    $submit = $('#submit'),
    $content = $('#content'),
    productId = $('#product_id').val(),
    isSubmiting;

$submit.on('touchend', function() {
    $content.blur();
    $consultForm.submit();
    return false;
}).on('touchstart', function() {
    $(this).addClass('highlight');
}).on('touchend touchcancel', function() {
    $(this).removeClass('highlight');
});

$content.on('focus', function() {
    if ($content.val() === '请输入咨询内容') {
        $content.val('');
    }
}).on('blur', function() {
    if ($content.val() === '') {
        $content.val('请输入咨询内容');
    }
});

// 提交表单请求
$consultForm.on('submit', function() {
    var content;

    if (isSubmiting) {
        return false;
    }

    // 简单的表单校验
    content = $content.val();
    if (!content || content === '请输入咨询内容') {
        tip.show('咨询内容不能为空');
        return false;
    }

    isSubmiting = true;
    loading.showLoadingMask();
    $.ajax({
        method: 'POST',
        url: '/product/detail/consultsubmit',
        data: {
            product_id: productId,
            content: content
        }
    }).then(function(res) {
        if ($.type(res) !== 'object') {
            res = {};
        }
        if (res.code !== 200) {
            tip.show(res.message || '网络出了点问题~');
            isSubmiting = false;
            loading.hideLoadingMask();
        } else {
            tip.show('提交成功~');
            setTimeout(function() {
                window.history.go(-1);
            }, 3000);
        }
    }).fail(function() {
        tip.show('网络出了点问题~');
        isSubmiting = false;
    });
    return false;
});