html代码:
<form>
<div class="form-group" style="position: relative">
<button class="btn btn-default">上传图片</button>
<!--用文件上传插件把文件提交到后台,再转移到前端指定的文件里-->
<input type="file" name="pic1">
<!--把文件地址提交到后台,存储起来-->
<input type="hidden" name="brandLogo">
</div>
<button type="submit">提交</button>
</form>
js代码:
var initFileUpLoad = function () {
// 初始化上传插件
$('[name="pic1"]').fileupload({
// 文件提交到的地址
url: '/category/addSecondCategoryPic',
//指定后台返回数据类型
dataType: 'json',
// 上传成功
done: function (e, data) {
console.log(data)
window.picAddr = data.result.picAddr
$('.picture').attr('src', data.result.picAddr)
$('[name="brandLogo"]').val(data.result.picAddr)
}
});
}
//初始化文件上传插件:
initFileUpLoad()
// 点击确认提交,一级分类id,二级分类,品牌logo
$('.submit').on('click', function (e) {
//阻止页面跳转的默认事件
e.preventDefault()
var message = ''
// $('.modalCategory').modal('hide')
var picAddr = $.trim($('[name="brandLogo"]').val())
var brandName = $.trim($('.secondCategoryName').val())
var categoryId = $.trim($('[name="categoryId"]').val())
if (categoryId.length == 0) {
message = '请选择一级分类!'
$('.alert-danger').remove()
var html1 = '<div class="alert alert-danger" role="alert">'+message+'</div>'
$('.message-danger').append(html1)
return false
}
if (brandName.length == 0) {
message = '请输入二级分类名称!'
$('.alert-danger').remove()
var html1 = '<div class="alert alert-danger" role="alert">'+message+'</div>'
$('.message-danger').append(html1)
return false
}
if (picAddr.length == 0) {
message = '请上传图片!'
$('.alert-danger').remove()
var html1 = '<div class="alert alert-danger" role="alert">'+message+'</div>'
console.log(html1)
$('.message-danger').append(html1)
return false
}
var $form = $(e.target);
$.ajax({
type:'post',
url:' /category/addSecondCategory',
data:$form.serialize(),
dataType:'json',
success:function (data) {
if(data.success == true){
window.page = 1;
render();
$('#save').modal('hide');
}
}
});
})