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 num = parseInt(prompt("请输入新增数量,最多只能是100", 30));
if(isNaN(num) || !num) return;
if(num > 100) num = 100;
var date = new Date();
date = prompt("请输入起始日期", date.format("yyyy-MM-dd"));
if(!date) return;
var stock = prompt("请输入默认库存", 9999);
if(!stock) return;
var original_price = prompt("请输入默认原价");
if(!original_price) return;
var price = prompt("请输入默认售价");
if(!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};
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