-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathvals.js
81 lines (70 loc) · 2.09 KB
/
vals.js
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
!function($) {
'use strict';
// if group is group then use multiple selection model.
// if group is false then use single selection model.
// if group is undefined then use single selection model execept for this.type === radio
$.fn.vals = function(group) {
var input = $(this);
var type = input.attr('type');
var isSelect = input.is('select');
var isCheckbox = (type === 'checkbox');
var isRadio = (type === 'radio');
var isNumber = (type === 'number');
var isFile = (type === 'file');
var name = input.attr('name');
var selector = '[name="' + name + '"]';
var val, idx;
if (isSelect) {
if (group) {
// multiple selection model
val = input.valsGroup(selector);
} else {
// single selection model
val = input.val();
}
} else if (isRadio) {
if (group || typeof group === 'undefined') {
// multiple selection model
selector = selector + ':checked';
val = input.valsGroup(selector);
} else {
// single selection model
val = input.filter(':checked').val();
}
} else if (isCheckbox) {
if (group) {
// multiple selection model
selector = selector + ':checked';
val = input.valsGroup(selector);
} else {
// single selection model
val = input.filter(':checked').val();
}
} else if (isNumber && typeof input.validity !== 'undefined') {
val = input.validity.badInput ? NaN : input.val();
} else if (isFile) {
val = input.val();
// Modern browser (chrome & safari)
if (val.substr(0, 12) === 'C:\\fakepath\\') val = val.substr(12);
// Legacy browsers, unix-based path
idx = val.lastIndexOf('/');
if (idx >= 0) val = val.substr(idx + 1);
// Windows-based path
idx = val.lastIndexOf('\\');
if (idx >= 0) val = val.substr(idx + 1);
} else if (input.attr('contenteditable')) {
val = input.text();
} else {
//val = input.val();
if (group) {
// multiple selection model
val = input.valsGroup(selector);
} else {
// single selection model
val = input.val();
}
}
if (typeof val === 'string') return val.replace(/\r/g, '');
return val;
};
}(window.jQuery);