$ .get(0)是什么意思? [英] What does $.get(0) mean?

查看:765
本文介绍了$ .get(0)是什么意思?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I saw it on the stripe code, but i haven't used it this way on a form submission.

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元素. – 火箭危险品

Check the docs: api.jquery.com/get $form.get(0) is the same as $form[0]. It get the DOM element at position 0 out of the jQuery object. – Rocket Hazmat

在这种情况下,它用于提交表单.

In this context it use used to submit the form.

您看到,当您在jQuery元素上使用.submit()时,会触发jQuery方法.submit(),该方法与.trigger('submit')相同

You see, when you use .submit() on a jQuery Element, you trigger the jQuery method .submit() which is the same as .trigger('submit') (ref. here). Using get(0).submit() here will the submit method of HTMLDomElement, which is the native form submit.

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

Why would you use the native submit instead of the jQuery submit? Simply because the native submit doesnt trigger .on('submit') or other event bind on submit before actually submitting the form.

这篇关于$ .get(0)是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆