Date.prototype.format = function (fmt) { var o = { 'M+': this.getMonth() + 1, //月份 'd+': this.getDate(), //日 'h+': this.getHours(), //小时 'm+': this.getMinutes(), //分 's+': this.getSeconds(), //秒 'q+': Math.floor((this.getMonth() + 3) / 3), //季度 'S': this.getMilliseconds() //毫秒 }; if (/(y+)/.test(fmt)) { fmt = fmt.replace(RegExp.$1, (this.getFullYear() + '').substr(4 - RegExp.$1.length)); } for (var k in o) { if (new RegExp('(' + k + ')').test(fmt)) { fmt = fmt.replace(RegExp.$1, (RegExp.$1.length === 1) ? (o[k]) : (('00' + o[k]).substr(('' + o[k]).length))); } } return fmt; }; $(function () { $('.has-many-spec .add.btn') .after('
 删除所有
') .after('
 批量新增
'); //批量新增 $('.has-many-spec .batch-add').click(function () { var name = prompt('请输入规格名称'); if (!name) { return; } var date = new Date(); date = prompt('请输入起始日期', date.format('yyyy-MM-dd')); if (!date || isNaN(new Date(date).getTime())) { return; } var num = parseInt(prompt('请输入新增数量,最多不超过180', '30')); if (!num || isNaN(num)) { return; } if (num > 180) { num = 180; } var stock = parseInt(prompt('请输入默认库存', '9999')); if (!stock || isNaN(stock)) { return; } var original_price = parseFloat(prompt('请输入默认市场价')); if (!original_price || isNaN(original_price)) { return; } var price = parseFloat(prompt('请输入默认销售价')); if (!price || isNaN(price)) { return; } var cost_price = parseFloat(prompt('请输入默认成本价')); if (!cost_price || isNaN(cost_price)) { return; } $('template.spec-tpl .field_name').prop('value', name); var html = $('template.spec-tpl').html(); var fields = { 'name': name, 'date': date, 'stock': stock, 'original_price': original_price, 'price': price, 'cost_price': cost_price, }; for (var key in fields) { html = html.replace('name="spec[new___LA_KEY__][' + key + ']" value=""', 'name="spec[new___LA_KEY__][' + key + ']" value="' + fields[key] + '"'); } var nestedIndex = $('.has-many-spec-forms tr').length - 1; function replaceNestedFormIndex(value) { return String(value).replace(/__LA_KEY__/g, nestedIndex); } for (var i = 0; i < num; i++) { nestedIndex++; var d = new Date(date); d.setDate(d.getDate() + i); var template = replaceNestedFormIndex(html.replace(date, d.format('yyyy-MM-dd'))); $('.has-many-spec-forms').append(template); } }); //清空全部 $('.has-many-spec .batch-sub').click(function () { $('.has-many-spec-forms .remove.btn').click(); }); });