我正在尝试通过表单提交数据,当点击提交按钮时,我希望某些图片随style.display = 'none';
消失。目前,我必须在图像消失之前单击按钮两次。有没有办法在第一次点击时执行此操作?
HTML:
<form action="/create_post/" method="POST" id="post-form">
<div class="col-sm-4" id="toprow">
<h4 style="font-family:verdana"> Models </h4>
<img src='{% static 'images/USAcomplete2.png' %}' class="img-responsive thumbnail" id='gbimg' >
<div class="btn-toolbar">
<button type="submit" name="model" value="test" class="btn btn-default">test</button>
<button type="submit" name="model" value="test2" class="btn btn-default">test2</button>
<button type="submit" name="model" value="test3" class="btn btn-default">test3</button>
</div>
</div>
</form>
JS:
$('#post-form').on('submit', function(event) {
event.preventDefault();
console.log("form submitted!")
create_post();
});
function create_post() {
console.log("create post is working!");
$("button").click(function() {
var cbtn = $(this);
var btnval = cbtn.val();
console.log(btnval);
document.getElementById('gbimg').style.display = 'none';
});
//$.ajax({
// url : "create_post/",
// type : "POST",
// data : { model :
};
答案 0 :(得分:2)
如果您的<button>
执行表单的submit
函数,则只需在提交表单时删除按钮,而不是在create_post()
方法中添加点击处理程序。所以:
$('#post-form').on('submit', function(event) {
event.preventDefault();
console.log("form submitted!");
var cbtn = $("button");
var btnval = cbtn.val();
console.log(btnval);
document.getElementById('gbimg').style.display = 'none';
create_post();
});
function create_post() {
console.log("create post is working!");
//$.ajax({
// url : "create_post/",
// type : "POST",
// data : { model :
};
答案 1 :(得分:1)
将按钮点击处理程序移到create_post函数之外:
$(document).ready(function () {
$('#post-form').on('submit', function(event) {
event.preventDefault();
console.log("form submitted!")
create_post();
});
function create_post() {
console.log("create post is working!");
var cbtn = $("button");
var btnval = cbtn.val();
console.log(btnval);
document.getElementById('gbimg').style.display = 'none';
//$.ajax({
// url : "create_post/",
// type : "POST",
// data : { model :
};
});