【问题标题】:Taking JavaScript variables of text and using them as hidden inputs also获取文本的 JavaScript 变量并将它们用作隐藏输入
【发布时间】: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


【解决方案1】:

如果我正确理解了您的问题,那么您只需要创建一个包含数据的对象,如下所示...

var ajaxData =  {
    color: gColor,
    category: gCategory,
    comment: gComment
};

然后在 Ajax 调用中将其作为您的数据对象传递...

$.ajax({
    url: "your-url",
    data: ajaxData
});

至于您关于颜色名称和数字在同一字符串中的问题,您可以像这样拆分它们......

var value = "112 - Brown";

// Split the string...  (use split("/") if it's a forward-slash)
val1 = val1.split(" - ");

var colorNum = val1[0];
var colorName = val1[1];

【讨论】:

  • 好的,所以它不一定是隐藏输入,因为我猜它已经被捕获了?所以我的表单围绕模态,我可以将提交按钮保留在那里,如果提交,我将像通常在 ajax 中那样传递数据
  • 只要您在表单中填充了输入,那么数据就会被传递,是的。这只是决定哪个是最适合您的解决方案的一个案例。
  • 那是我的困惑。通常我会有输入,但这里需要的文本是一个静态标题,这就是为什么我将它的文本作为 ID 抓取。所以无论哪种方式,我都得到了我需要的数据
  • 隐藏字段是一种更简单的方法,如果您已经提交了一个表单,并且您没有其他理由考虑使用 Ajax。
  • 明白了,谢谢,我认为这会起作用,谢谢!另外,如果我的颜色字段返回“112 - Brown”或“112/Brown”,有没有办法用 /,- 分割它们,并将第一个作为 colorNum,第二个作为 colorName?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多