Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
116 views
in Technique[技术] by (71.8m points)

javascript - HTML and jQuery Modal for Editing

I am somehow new to jQuery and is going through some code of a project.

The existing jQuery looks like this and this pops up a modal of confirmation upon delete:

{% for profile in profiles %}

        $("#delete_{{ profile.id }}").mousedown(function(event){
        let action = "deleteprofile"
        $.confirm({
            title: 'Delete',
            content: 'Are you sure?',
            buttons: {
                confirm: function () {
                    submit_to_modal({
                            'profile_id': "{{ profile.id }}"
                        },
                        action)
                },
                cancel: function () {
                    $.alert('cancel');
                }
            }
        });
    })

{% endfor %}

I think this project uses jquery-confirm after searching for similarities in the code, I read about it here: https://craftpip.github.io/jquery-confirm/ it can also be found here: How to display a confirm box before submitting a form using jquery confirm?

So upon the click of a button in the html. e.g

  <button id="delete_{{ profile.id }}">Delete</button>

The jQuery will be triggered producing a confirmation. After confirmation the deleteprofile action which is from the views of the backend will be triggered and it will make the DELETE request.

However, I want to be able to make an edit modal from this.

Where an item from a list of items, such as profiles when clicked will produce a modal.

I searched for it and found these resources:

Bootstrap: Modal dialog for editing data dynamically Jquery .on() submit event

But all of them don't receive the which will trigger the specific item. For example, I only want to edit, profile-001, but it can't be done with these samples. I already tried them and the modal didn't pop out.

I found this: How to make an "Edit User" form inside Bootstrap Modal using JQUERY?

But this is on PHP, I am using only jinja for this. I also couldn't replicate this on the html as I am fairly new to frontend stuff.

How do you usually add input fields to modals in jinja, or in jQuery in this case? I want to be able to pop out a modal which will have input values, which will be received by jQuery per item.

Any source to what I want to find is also welcome, I have been scanning four hours already and tried testing it but I can't get a modal to pop up except for the deletion modal.

question from:https://stackoverflow.com/questions/65855042/html-and-jquery-modal-for-editing

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Use jQuery-ui cdn, Here is the code for dialog.

ConfirmDialog('Are you sure'); //need to call this function with click, "Are you sure" 
                                 message passed with function

ConfirmDialog('Are you sure');

function ConfirmDialog(message) {
  $('<div></div>').appendTo('body')
    .html('<div><h6>' + message + '?</h6></div>')
    .dialog({
      modal: true,
      title: 'Your Title...',
      zIndex: 10000,
      autoOpen: true,
      width: 'auto',
      resizable: false,
      buttons: {
        Confirm: function() {

          $('body').append('<h3>Confirm Dialog Result: <b>Confirm</b></h3>');

          $(this).dialog("close");
        },
        Cancel: function() {
          $('body').append('<h3>Confirm Dialog Result: <b>Cancel</b></h3>');

          $(this).dialog("close");
        }
      },
      close: function(event, ui) {
        $(this).remove();
      }
    });
};
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<link href="https://ajax.googleapis.com/ajax/libs/jqueryui/1.12.1/themes/smoothness/jquery-ui.css" rel="stylesheet" />
<script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.12.1/jquery-ui.min.js"></script>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...