$ .get(0)是什么意思?

时间:2014-03-27 18:58:39

标签: jquery

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

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();
  }

2 个答案:

答案 0 :(得分:3)

如评论所述:

  

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

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

您会看到,当您在jQuery元素上使用.submit()时,会触发与.submit() (ref. here)相同的jQuery方法.trigger('submit')。在get(0).submit()处使用HTMLDomElement的提交方法,即本机表单提交。

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

答案 1 :(得分:0)

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

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