Javascript 通过jQuery获取html表单

Javascript 通过jQuery获取html表单,javascript,jquery,html,Javascript,Jquery,Html,我正在尝试从jQuery获取表单,但找不到解决方案。 我尝试了一些示例,但它们没有显示表单标记及其属性 我的页面上有三个表单,我想使用第二个表单。当我使用此代码时,它会显示第一个表单数据。请告诉我在我的页面上获取第二个表单以及表单元素的解决方案 我想从jQuery获得这个表单,我使用了这个代码,但是这个代码给了我表单中的元素,而不是表单标签 $(document).ready(function() { $('a').on('click', function() { a

我正在尝试从jQuery获取表单,但找不到解决方案。 我尝试了一些示例,但它们没有显示表单标记及其属性


我的页面上有三个表单,我想使用第二个表单。当我使用此代码时,它会显示第一个表单数据。请告诉我在我的页面上获取第二个表单以及表单元素的解决方案

我想从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>

精选产品

我相信这就是您想要的:

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

获取第二种形式:

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

最好给表单一个唯一的
id

您可以将其包装在一个div中,跳转到包装div的某个级别,并获取该div的内容:

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

这可能会有所帮助:我的页面上有三个表单,我想使用第二个表单。当我使用此代码时,它会显示第一个表单。请告诉我在我的页面上获取第二个表单的解决方案。