【问题标题】:What does $.get(0) mean?$.get(0) 是什么意思?
【发布时间】:2014-03-28 03:21:50
【问题描述】:

我在条带代码上看到了它,但我没有在表单提交中以这种方式使用它。

https://gist.github.com/briancollins/6365455

使用.get(0)的原因是什么?

var stripeResponseHandler = function(status, response) {
  var $form = $('#payment-form');

  if (response.error) {
    // Show the errors on the form
    $form.find('.payment-errors').text(response.error.message);
    $form.find('button').prop('disabled', false);
  } else {
    // token contains id, last4, and card type
    var token = response.id;
    // Insert the token into the form so it gets submitted to the server
    $form.append($('<input type="hidden" name="stripeToken" />').val(token));
    // and re-submit
    $form.get(0).submit();
  }

【问题讨论】:

  • 查看文档:api.jquery.com/get $form.get(0)$form[0] 相同。它从 jQuery 对象中获取位置 0 的 DOM 元素。
  • @RocketHazmat 因为这里没有更好(或更多参与)的答案..

标签: jquery


【解决方案1】:

正如评论中所说:

查看文档:api.jquery.com/get $form.get(0)$form[0] 相同。它从 jQuery 对象中获取位置 0 的 DOM 元素。 – Rocket Hazmat

在此上下文中,它用于提交表单。

你看,当你在一个 jQuery 元素上使用 .submit() 时,你会触发 jQuery 方法 .submit(),它与 .trigger('submit') (ref. here) 相同。这里使用get(0).submit()会使用HTMLDomElement的submit方法,也就是原生表单提交。

为什么要使用原生提交而不是 jQuery 提交?仅仅是因为原生提交不会触发.on('submit') 或提交时的其他事件绑定在实际提交表单之前

【讨论】:

    【解决方案2】:

    它从 jQuery 对象中获取第一个匹配的 DOM 元素

    http://api.jquery.com/get/

    【讨论】:

      猜你喜欢
      • 2015-03-20
      • 2020-01-07
      • 2018-09-10
      • 2013-03-07
      • 2017-05-09
      • 2018-09-25
      • 2011-05-25
      • 2010-11-20
      • 1970-01-01
      相关资源
      最近更新 更多