我希望有人可以帮助我。
我的表单中有两个按钮。 "保存"和"发布"。这是HTML:
<button type="submit" class="button">Save</button>
<button type="button" class="button" name="publish" value="true" onclick="publishAlbum({{ album.id }}, '{{ album.title }}')">Publish</button>
第一个保存专辑,第二个发送电子邮件给所有者。第二个(&#34; Publish&#34;)需要首先触发确认(&#34;你确定吗?&#34;)。当您单击&#34;确定&#34;时,表单应该提交,但如果您单击&#34;取消&#34; (在确认框中),它什么都不做。
这是我的JS:
function publishAlbum(album_id, album_title)
{
var result = confirm('Are you sure you want to publish this album?');
if(!result)
{
return;
}
}
我尝试了所有内容(防止默认,返回等),但每次点击&#34;取消&#34;,表单仍然提交并发送电子邮件。
有人可以帮助我吗?
答案 0 :(得分:0)
发布
$('.publish-button').on('click',function(e){
e.preventDefault();
let albumId = $('#selectYourAlbumId');
let albumTitle = $('#selectYourAlbumTitle');
var result = confirm('Are you sure you want to publish this album?');
if(!result)
{
return;
}
// POST your form through an AJAX call
})
答案 1 :(得分:0)
您需要以某种方式获取事件对象(例如,通过向按钮添加事件侦听器)。然后你可以阻止表单提交,如下所示:
const album = {
id: 1,
title: 'Test',
};
document.querySelector('[name=publish]').addEventListener('click', function(e) {
if (!publishAlbum(album.id, album.title)) {
e.preventDefault();
}
});
function publishAlbum(album_id, album_title) {
var result = confirm('Are you sure you want to publish this album?');
if (!result) {
return false;
}
// do your stuff
return true;
}
&#13;
<form action="https://example.org" method="POST">
<button type="submit" class="button">Save</button>
<input type="submit" class="button" name="publish" value="Publish" />
</form>
&#13;
答案 2 :(得分:0)
假设您在表单标记中包含这些按钮,可以尝试:
<html>
<body>
<h2>JavaScript Confirm Box</h2>
<button type="submit" class="button">Save</button>
<button type="button" class="button" name="publish" value="true" onclick="publishAlbum()" id="myButton">Publish</button>
<script>
function publishAlbum() {
var txt;
if (confirm("Press a button!") == true) {
$("#myButton").trigger('submit');
} else {
txt = "You pressed Cancel!";
alert(txt)
}
}
</script>
</body>
</html>
答案 3 :(得分:0)
我用过这个:
import urllib.request
class AppURLopener(urllib.request.FancyURLopener):
version = "Mozilla/5.0"
opener = AppURLopener()
fh = opener.open('http://www.cmegroup.com/tools-information/quikstrike/treasury-analytics.html')