将变量从模态框 A 传递到模态框 B

huangapple go评论88阅读模式
英文:

Pass Variable From Modal A To Modal B

问题

  1. $(document).on("click", "#btnModifyVisit", function(e) {
  2. console.log('modify button clicked');
  3. //row id of clicked button
  4. var id = $(this).attr("data-id");
  5. // Button that triggered the modal
  6. var buttonClicked = $(this).data('bs-whatever');
  7. console.log(id);
  8. console.log(buttonClicked);
  9. });
英文:

Working on building a approval/edit process for company site. I have it set-up so that a jquery datatable displays scheduled events for the logged in user and displays the trip like this

  1. results = await response.json();
  2. var tr = $(this).closest('tr');
  3. var id = tr.children('td:eq(0)').text()
  4. tbl = $('#example').DataTable( {
  5. dom: 'lBfrtip',
  6. lengthMenu: [ [10, 25, 50, 100, -1], [10, 25, 50, 100, "All"] ],
  7. pageLength: 50,
  8. data: results,
  9. columns: [
  10. {
  11. title: "Arrived",
  12. data: "id" ,
  13. width: "10%",
  14. className: "text-center",
  15. render: function (data, type, full, meta){
  16. return '<input type="checkbox" name="id[]" value="' + $('<div/>').text(data).html() + '">';
  17. }
  18. },
  19. {
  20. title: "Employee Name",
  21. data: "Employee",
  22. width: "20%"
  23. },
  24. {
  25. title: "Destination",
  26. data: "Destination"
  27. },
  28. {
  29. title: "Modify",
  30. render: function(data, type, row, meta)
  31. {
  32. return '<button id="btnModify" data-id="' + row.id + '" data-bs-toggle="modal" data-bs-target="#modifyVisit" class="btn btn-success" type="button">Modify Visit</button>';
  33. }
  34. }
  35. ]
  36. });
  37. }
  38. }

this part works excellent - now I am trying to modify this a bit and add the Modify button in the jquery datatable - that piece is done the button is there, YAY!

The issue that when the Modify button is pressed a modal is displayed that gives the user the option to either Reschedule Or Cancel. Here is the html for that

  1. <div id="modifyVisit" class="modal" tabindex="-1">
  2. <div class="modal-dialog">
  3. <div class="modal-content">
  4. <div class="modal-header">
  5. <h5 class="modal-title">Modify Visit</h5>
  6. <button class="close" type="button" data-dismiss="modal"> × </button></div>
  7. <div class="modal-body">
  8. <h2>Select The Modification Type</h2>
  9. <div class="wrap">
  10. <div class="row">
  11. <div class="col-xs-6 col-xl-6 item"><button id="btnReschedule" class="btn btn-primary" type="button" data-bs-whatever="reschedule">Reschedule Visit</button></div>
  12. <div class="col-xs-6 col-xl-6 item"><button id="btnCancel" class="btn btn-primary" type="button" data-bs-whatever="cancel">Cancel Visit</button></div>
  13. </div>
  14. <div class="modal-footer"><button class="btn btn-secondary" type="button" data-dismiss="modal">Close</button></div>
  15. </div>
  16. </div>
  17. </div>
  18. </div>

my issue is that I need to capture which button was pressed (so I know which data points to display) and also the id of the row that the user clicked the modify button on.

I tried to capture it like this

  1. var modifyVisit = document.getElementById('#btnModify')
  2. modifyVisit.addEventListener('show.bs.modal', function (event) {
  3. //row id of clicked button
  4. var id = $(this).attr("data-id");
  5. // Button that triggered the modal
  6. var button = event.relatedTarget
  7. // Extract info from data-bs-* attributes
  8. var buttonClicked = button.getAttribute('data-bs-whatever')
  9. console.log(id);
  10. console.log(buttonClicked);
  11. });

but I get a console error, I assume because I'm trying to add an event listener to an element that is not displayed on the page when the page loads...

  1. caught TypeError: Cannot read properties of null (reading 'addEventListener')
  2. at HTMLDocument.<anonymous>

I know I can use var id = $(this).attr("data-id"); to get the id of the row where the button was pressed, but what have I set-up incorrectly here?

<strong>EDIT</strong><br>
I edited the click to use jquery like this

  1. $(document).on(&quot;click&quot;, &quot;#btnModifyVisit&quot;, function(e) {
  2. console.log(&#39;modify button clicked&#39;);
  3. //row id of clicked button
  4. var id = $(this).attr(&quot;data-id&quot;);
  5. // Button that triggered the modal
  6. var button = e.relatedTarget
  7. // Extract info from data-bs-* attributes
  8. var buttonClicked = e.getAttribute(&#39;data-bs-whatever&#39;)
  9. console.log(id);
  10. console.log(buttonClicked);
  11. });

and now it is giving me an error on click of

  1. caught TypeError: e.getAttribute is not a function

答案1

得分: 0

You are exactly right. It can't listen to an element that does not exist when the page loads. jQuery has a very good solution for this that I use for everything - .on().

Also I think the show.bs.modal event happens on the modal element.

Try the below code.

  1. $(document).on('show.bs.modal', '.modal', function (event) {
  2. //row id of clicked button
  3. var id = $(this).attr("data-id");
  4. // Button that triggered the modal
  5. var button = event.relatedTarget
  6. // Extract info from data-bs-* attributes
  7. var buttonClicked = button.getAttribute('data-bs-whatever')
  8. console.log(id);
  9. console.log(buttonClicked);
  10. });

This essentially works behind the scenes to attach the listener to the document and check to see if the target equals the second parameter (in this case #btnModify). It allows you to have listeners for dynamically added content, which is exactly what you need.

英文:

You are exactly right. It can't listen to an element that does not exist when the page loads. jQuery has a very good solution for this that I use for everything - .on().

Also I think the show.bs.modal event happens on the modal element.

Try the below code.

  1. $(document).on(&#39;show.bs.modal&#39;, &#39;.modal&#39;, function (event) {
  2. //row id of clicked button
  3. var id = $(this).attr(&quot;data-id&quot;);
  4. // Button that triggered the modal
  5. var button = event.relatedTarget
  6. // Extract info from data-bs-* attributes
  7. var buttonClicked = button.getAttribute(&#39;data-bs-whatever&#39;)
  8. console.log(id);
  9. console.log(buttonClicked);
  10. });

This essentially works behind the scenes to attach the listener to the document and check to see if the target equals the second parameter (in this case #btnModify). It allows you to have listeners for dynamically added content, which is exactly what you need.

huangapple
  • 本文由 发表于 2023年5月21日 06:37:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/76297598.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定