2014-03-27 223 views
0

我在條形碼上看到了它,但我沒有在表單提交中以這種方式使用它。

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

檢查文檔:http://api.jquery.com/get/「$ form.get(0)'與$ form [0]'相同。它從jQuery對象中獲取'0'位置的DOM元素。 –

+0

@RocketHazmat因爲這裏沒有更好的(或者更多的參與)答案.. – user2864740

回答

3

正如評論說:

檢查文檔:api.jquery.com/get$form.get(0)相同$form[0] 。它在位置0以外的jQuery對象中獲取DOM元素。 - Rocket Hazmat

在這種情況下用它來提交表單。

您會發現,當您在jQuery元素上使用.submit()時,會觸發與.trigger('submit')(ref. here)相同的jQuery方法.submit()。使用get(0).submit()這裏將提交HTMLDomElement的提交方法,這是本地表單提交。

爲什麼你會使用本地提交而不是jQuery提交?僅僅是因爲在實際提交表格之前,原始提交併未觸發.on('submit')或其他事件綁定提交