我正在从'myData'数组中创建每个对象的div。 另外,我有一个bootstrap模式,它有一个表单来添加一个外部对象。 这个外部对象将被添加到我的数组'myData'中,并以与上述相同的格式显示它。
我的问题是,在成功创建外部对象后,我应该能够 -
单击“保存”按钮关闭模态并成功创建外部对象。
每当我点击“添加外部对象”按钮时,模态中的表单不应包含任何预先打印的内容。
我尝试使用reset方法执行此操作,但它似乎不起作用。 你能帮我解决一下吗? 这是我的完整代码 -
var myData = [{
company: "ABC",
url: "www.abc.com",
type: "internal"
}, {
company: "CDE",
url: "www.cde.com",
type: "internal"
}, {
company: "DEF",
url: "www.def.com",
toolName: "reportTool",
toolURL: "http://www.toolURL.com",
type: "internal"
}, {
company: "EFG",
url: "www.efg.com",
type: "internal"
}, {
company: "FGH",
url: "www.fgh.com",
type: "internal"
}];
$('#createData').click(function() {
createDisplay();
});
function createDisplay() {
$('.container').empty();
myData.forEach(function(obj) {
$('.container').append(
$('<div>').addClass('box').append(
$('<label>').text('Company Website: '),
$('<a>').addClass('compUrl').attr('href', obj.url).text(obj.company),
)
)
});
}
var objIndex;
$(document).ready(function(){
var $toolName = $('#newToolName');
var $toolURL = $('#newToolUrl');
var $toolInputs = $($toolName).add($toolURL);
$toolInputs.on('change', function(e) {
var toolName = $toolName.val();
var toolURL = $toolURL.val();
$toolInputs.prop('required', toolName || toolURL);
});
$('#createForm').submit(function(e){
var toolName = $toolName.val()
var toolURL = $toolURL.val()
var toolFilled = !!toolName && !!toolURL;
var toolUnfilled = !toolName && !toolURL;
if (toolFilled || toolUnfilled) {
createData(objIndex);
return false;
}
return false;
});
});
function createData() {
companyName = $('#companyName').val();
companyUrl = $('#companyUrl').val();
toolName = $('#newToolName').val();
toolURL = $('#newToolUrl').val();
var extObj = {
company: companyName,
url: companyUrl,
toolName: toolName,
toolURL: toolURL,
type: 'external'
}
myData.push(extObj);
createDisplay();
alert('Data created successfully!');
//$('#createForm').reset();
}
.box {
height: 100px;
background-color: skyblue;
border: 1px solid black;
border-radius: 4px;
margin-top: 5px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.0/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/>
<div class="container">
</div>
<button id="createData" class="btn btn-info">Create divs</button>
<button type="button" class="btn btn-info" data-toggle="modal" data-target="#extData">Add external object</button>
<!-- Modal -->
<div class="modal fade" id="extData" tabindex="-1" role="dialog" aria-labelledby="update-data" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<!-- Modal Header -->
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">
<span aria-hidden="true">×</span>
<span class="sr-only">Close</span>
</button>
<h4 class="modal-title" id="myModalLabel">Update Data</h4>
</div>
<form id="createForm">
<!-- Modal Body -->
<div class="modal-body">
<div class="form-group">
<label class="col-sm-4 control-label" for="companyName">Company Name</label>
<div class="col-sm-8">
<input type="text" class="form-control" id="companyName" placeholder="Company Name" required />
</div>
</div>
<div class="form-group">
<label class="col-sm-4 control-label" for="companyUrl">Website</label>
<div class="col-sm-8">
<input type="url" class="form-control" id="companyUrl" placeholder="Company URL" required />
</div>
</div>
<div class="form-group">
<label class="col-sm-4 control-label" for="newToolName">Tool Name</label>
<div class="col-sm-8">
<input type="text" class="form-control" id="newToolName" />
</div>
</div>
<div class="form-group">
<label class="col-sm-4 control-label" for="newToolUrl">Tool URL</label>
<div class="col-sm-8">
<input type="url" class="form-control" id="newToolUrl" />
</div>
</div>
</div>
<!-- Modal Footer -->
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
<button class="btn btn-primary">Save</button>
</div>
</form>
</div>
</div>
</div>
答案 0 :(得分:1)
将此代码添加到createData
函数,以便在创建数据时关闭模式:
$('#extData').modal('hide');
之后,重置表单如下:
$('#createForm')[0].reset();
根据w3schools documentation这需要在formObject上完成,你在jQuery对象上尝试过。