consultform.js
2.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
/**
* 我要咨询提交页面
* @author: liangzhifeng<zhifeng.liang@yoho.cn>
* @date: 2015/12/01
*/
var $ = require('yoho-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('请输入咨询内容');
}
});
var getUrlParam = function (paramName) {
var reg = "[\\?|\\&]+" + paramName + "=([^&]*)[^&]?";
var exp = (window.location.href + "").match(reg);
return exp ? exp[1] : "";
}
// 提交表单请求
$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() {
var from = getUrlParam('from');
if (from) {
window.location = decodeURIComponent(from);
} else {
window.history.go(-1);
}
}, 3000);
}
}).fail(function() {
tip.show('网络出了点问题~');
isSubmiting = false;
});
return false;
});