2013-11-24 60 views
0

我想從jQuery獲取表單,並找不到解決方案。 我已經嘗試過一些例子,但他們不顯示錶格標籤及其屬性。通過jQuery獲取HTML表格

Here is a sample fiddle

我有我的網頁上有三種形式,我想。當我使用此代碼,它讓我看到第一種形式data.Can請你告訴我的解決方案上獲得第二形式採取第二種形式我的頁面和表單元素。

我想從jQuery中獲得這個表單,而且我已經使用了這段代碼,但是這段代碼給了我表單中的元素而不是表單標籤。

$(document).ready(function() 
{ 
    $('a').on('click', function() { 
     alert($('form').html()); 
    }); 
}); 

的形式是

<form class="variations_form cart" method="post" enctype="multipart/form-data" data-product_id="1142" data-product_variations="[{&quot;variation_id&quot;:&quot;1153&quot;,&quot;attributes&quot;:[],&quot;image_src&quot;:&quot;http:\/\/maza-it.net\/tilde\/wp-content\/uploads\/2013\/10\/H07-300x300.jpg&quot;,&quot;image_link&quot;:&quot;http:\/\/maza-it.net\/tilde\/wp-content\/uploads\/2013\/10\/H07.jpg&quot;,&quot;image_title&quot;:&quot;H07&quot;,&quot;image_alt&quot;:&quot;&quot;,&quot;price_html&quot;:&quot;&lt;span class=\&quot;price\&quot;&gt;&lt;span class=\&quot;amount\&quot;&gt;$26&lt;\/span&gt;&lt;\/span&gt;&quot;,&quot;availability_html&quot;:&quot;&lt;p class=\&quot;stock \&quot;&gt;19 in stock&lt;\/p&gt;&quot;,&quot;sku&quot;:&quot;555&quot;,&quot;weight&quot;:&quot; kg&quot;,&quot;dimensions&quot;:&quot;&quot;,&quot;min_qty&quot;:1,&quot;max_qty&quot;:&quot;19&quot;,&quot;backorders_allowed&quot;:false,&quot;is_in_stock&quot;:true,&quot;is_downloadable&quot;:false,&quot;is_virtual&quot;:false,&quot;is_sold_individually&quot;:&quot;no&quot;,&quot;image_magnifier&quot;:&quot;http:\/\/maza-it.net\/tilde\/wp-content\/uploads\/2013\/10\/H07-600x600.jpg&quot;},{&quot;variation_id&quot;:&quot;1154&quot;,&quot;attributes&quot;:[],&quot;image_src&quot;:&quot;http:\/\/maza-it.net\/tilde\/wp-content\/uploads\/2013\/10\/S05-300x300.jpg&quot;,&quot;image_link&quot;:&quot;http:\/\/maza-it.net\/tilde\/wp-content\/uploads\/2013\/10\/S05.jpg&quot;,&quot;image_title&quot;:&quot;S05&quot;,&quot;image_alt&quot;:&quot;&quot;,&quot;price_html&quot;:&quot;&lt;span class=\&quot;price\&quot;&gt;&lt;span class=\&quot;amount\&quot;&gt;$100&lt;\/span&gt;&lt;\/span&gt;&quot;,&quot;availability_html&quot;:&quot;&lt;p class=\&quot;stock \&quot;&gt;9 in stock&lt;\/p&gt;&quot;,&quot;sku&quot;:&quot;60&quot;,&quot;weight&quot;:&quot; kg&quot;,&quot;dimensions&quot;:&quot;&quot;,&quot;min_qty&quot;:1,&quot;max_qty&quot;:&quot;9&quot;,&quot;backorders_allowed&quot;:false,&quot;is_in_stock&quot;:true,&quot;is_downloadable&quot;:false,&quot;is_virtual&quot;:false,&quot;is_sold_individually&quot;:&quot;no&quot;,&quot;image_magnifier&quot;:&quot;http:\/\/maza-it.net\/tilde\/wp-content\/uploads\/2013\/10\/S05-600x600.jpg&quot;}]"> 
    <table class="variations" cellspacing="0"> 
     <tbody> 
        </tbody> 
    </table> 

    <div class="single_variation_wrap" style="display:none;"> 
     <div class="single_variation"></div> 
     <div class="variations_button"> 
      <input type="hidden" name="variation_id" value=""> 
      <div class="quantity buttons_added" style="display: none;"><input type="button" value="-" class="minus"><input type="number" step="1" name="quantity" value="1" title="Qty" class="input-text qty text"><input type="button" value="+" class="plus"></div> 
      <button type="submit" class="single_add_to_cart_button button alt">Select Product</button> 
     </div> 
    </div> 
    <div> 
     <input type="hidden" name="add-to-cart" value="1142"> 
     <input type="hidden" name="product_id" value="1142"> 
    </div> 

</form> 
+0

這可能幫助:http://stackoverflow.com/a/2419877/ 1238887 – ahren

+0

[獲取選定元素的外部HTML]的可能重複(http://stackoverflow.com/questions/2419749/get-selected-elements-outer-html) – ahren

回答

2

我相信這是你在找什麼:

alert($('form')[0].outerHTML); 

FIDDLE

獲取第二種形式:

$('form:eq(1)'); 

而不是給表單一個獨特的id

+0

我在頁面上有三種形式,我想採取第二種形式.WHen我使用這個代碼,它顯示了我的第一種形式。可以請告訴我在我的頁面上獲得第二種形式的解決方案。 –

+0

@hassaanshuja我更新了答案。 – plalx

0

你可以把它包在一個div,跳了一級包裝DIV,並獲得該內容:

$('form').wrap('<div/>').parent().html()