【发布时间】:2018-09-14 13:54:24
【问题描述】:
我有一些 JavaScript 可以从网站的不同区域填充模式。基本上,如果您单击展开模式的按钮,它将捕获与按下的按钮相关的所有数据。
这非常有效,但我需要获取当前用于填充我的类的文本元素,并将它们捕获为隐藏输入以传递给 AJAX 调用。
下面的 JavaScript 获取与单击的按钮相关的所有文本并将其作为 h3 类传递。我的 console.logs 正在转储我还需要作为隐藏输入的值。
我如何(在模态扩展时)捕获这些相同的元素作为隐藏输入并将它们传递给 AJAX 调用?
$('.expand-modalForDelete').click(function() {
var row = $(this).parent()[0],
allElementsInRow = $(row).find('div'),
gName = allElementsInRow[0].outerText,
gColor = allElementsInRow[1].outerText,
gCategory = allElementsInRow[2].outerText;
gComment = allElementsInRow[3].outerText;
$('#gName').text(gName);
$('#gColor').text(gColor);
$('#gCategory').text(gCategory);
$('#gComment').text(gComment);
console.log(gName);
console.log(gColor);
console.log(gCategory);
console.log(gComment);
UIkit.modal("#modalForDelete").show();
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="modalForDelete" class="uk-modal modalForDelete">
<div class="uk-modal-dialog">
<form id="editModal">
<div class="uk-width-1-1">
<div class="uk-width-1-1" style="margin-bottom:10px;">
<div class="uk-grid">
<h4 class="uk-width-4-10 uk-text-muted">Name</h4>
<h3 id="gName" class="uk-width-4-10"></h3>
</div>
</div>
<div class="uk-width-1-1" style="margin-bottom:10px;">
<div class="uk-grid">
<h4 class="uk-width-4-10 uk-text-muted">Color</h4>
<h3 id="gColor" class="uk-width-4-10"></h3>
</div>
</div>
<div class="uk-width-1-1" style="margin-bottom:10px;">
<div class="uk-grid">
<h4 class="uk-width-4-10 uk-text-muted">Category:</h4>
<h3 id="gCategory" class="uk-width-4-10"></h3>
</div>
</div>
<div class="uk-width-1-1" style="margin-bottom:10px;">
<div class="uk-grid">
<h4 class="uk-width-4-10 uk-text-muted">Comment:</h4>
<h3 id="gComment" class="uk-width-4-10"></h3>
<span class="edit-comment-icon uk-icon-pencil uk-width-2-10 uk-text-center" data-uk-tooltip="{cls:'edit-tooltip'}" title="Edit"></span>
<button class="save-comment-button uk-button uk-button-success uk-width-2-10 uk-hidden">Save</button>
</div>
</div>
</div>
<div class="uk-modal-footer uk-text-center">
<a id="delete-product-list-item" href="" class="uk-icon-button uk-icon-trash-o"></a>
</div>
</form>
</div>
</div>
AJAX:
data: /* same as $('#gName').text(gName);
$('#gColor').text(gColor);
$('#gCategory').text(gCategory);
$('#gComment').text(gComment);*/
【问题讨论】:
-
不确定问题出在哪里 - 您能否在 ajax 调用中使用您已经收集的相同数据?
标签: javascript jquery ajax forms