简体   繁体   English

按钮在html表单内不起作用,但在外部起作用

[英]Button doesn't work inside the html form but it works outside

Hi there I need some help with this html form! 嗨,我需要一些有关此html表单的帮助! I'm making a bulletin board using summernote ( https://summernote.org/examples/ ) 我正在使用summernote( https://summernote.org/examples/ )制作公告板

At first I made the button(save, edit) outside the form and it perfectly worked. 首先,我将按钮(保存,编辑)放置在表单外部,并且效果很好。

Clicking edit button shows the editor 单击编辑按钮显示编辑器

And the save button saves the change and hides the editor 保存按钮保存更改并隐藏编辑器

However as I moved it inside it wouldn't work. 但是,当我将其移入内部时,它将无法工作。 Can't figure out why... Are the elements inside the form independent from the rest? 无法弄清楚为什么...表单中的元素是否独立于其余元素?

If not, what did I do wrong? 如果没有,我做错了什么? And what should I change to make two buttons exist inside the editor form? 我应该怎么做才能使两个按钮存在于编辑器表单中?

Html that works : 有效的HTML:

<div class="container">
    <div class="page-header">
        <h1>Hello</h1>
    </div>
    <form class="edit-summit-form" method="post" action="      ">
            <div id="editor">

            </div>
    </form>
    <p>
        <!--<div id="summernote-result"></div>-->
    </p>
    <button id="edit" class="btn btn-primary" onclick="edit()" type="button">Edit 1</button>
    <button id="save" class="btn btn-primary" onclick="save()" type="button">Save 2</button>
    <div class="click2edit">click2edit</div>
</div>

Html that doesn't work : 无效的HTML:

<div class="container">
    <div class="page-header">
        <h1>Hello</h1>
    </div>
    <form class="edit-summit-form" method="post" action="      ">
            <div id="editor">
              <button id="edit" class="btn btn-primary" onclick="edit()" type="button">Edit 1</button>
              <button id="save" class="btn btn-primary" onclick="save()" type="button">Save 2</button>
              <div class="click2edit">click2edit</div>
            </div>
    </form>
    <p>
        <!--<div id="summernote-result"></div>-->
    </p>
</div>

Javascript : Javascript:

    $(document).ready(function() {
        $('#summernote').summernote();
    });
    var edit = function() {
      $('.click2edit').summernote({focus: true});
    };
    var save = function() {
      var markup = $('.click2edit').summernote('code');
      $('.click2edit').summernote('destroy');
    };

You haven't explained what "doesn't work", but I'm guessing your form is submitting and you don't want it to. 您尚未解释什么“无效”,但我想您的表单正在提交,而您不希望这样做。 Remove the onclick inline JS and have the event callback prevent form submission: 删除onclick内联JS,并让事件回调阻止表单提交:

$('#save').on('click', function(e) {
    e.stopPropagation();
    var markup = $('.click2edit').summernote('code');
    $('.click2edit').summernote('destroy');
    return false;
});

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 脚本标签在体内起作用,但在体外不起作用 - Script tag works inside the body but doesn't work outside 获取方法在 function 外部有效,但在 function 内部无效 - Fetch method works outside a function but doesn't work inside a function jQuery .on适用于第一个html,但不适用于第二个(相同格式) - JQuery .on works on first html but doesn't work on the second(same form) 窗体内具有onclick事件的普通Button不起作用 - normal Button with onclick event inside a form doesn't work HTML表单提交按钮不起作用,仅重新加载页面 - HTML Form Submit button doesn't work, only reloads page 数组排序方法在 cypress 外部不起作用,但在内部起作用 - array sort method doesn't work outside cypress each but works inside 我尝试在 html 中创建一个 On 和 Off 按钮,但是当我将标签输入放入表单时不起作用,它保持打开状态并不会切换到打开状态 - I try to make a On and Off button in html and but when I put the tag input inside the Form doesn't work it stay on off doesn't switch to On 当按钮在表单标签之外时,jQuery表单无法验证 - jQuery form doesn't validate when button is outside of form tags Javascript:在实际表单之外提交表单不起作用 - Javascript : Submitting a form outside the actual form doesn't work 当javascript在html内时,它在外面时起作用。 - When the javascript is within the html it works when it outside it doesn't
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM