页面上多次使用相同的表单,获取已提交表单的值

时间:2014-11-13 04:30:23

标签: javascript php jquery forms

我在页面上多次使用相同的表单,我尝试使用jquery来检测提交的表单并获取其值。但目前只有页面上的第一个表单有效,而其他表单不会触发。我该如何解决这个问题?

表单看起来像这样:

    <form id="prepare_payment_form" action="#" method="post">
        <input name="merchant_id" type="hidden" value="'.$paypal_acount.'"/>
        <input name="merchant_amount" type="hidden" value="'.$entity_price.'"/>
        <input name="site_owner_id" type="hidden" value="'.$site_owner_id.'"/>
        <input name="site_owner_commission" type="hidden" value="'.$site_owner_commission.'"/>
        <input name="currency_code" type="hidden" value="'.$entity_unit->currency.'"/>
        <input name="use_sandbox" type="hidden" value="'.$use_sandbox.'"/>
        <input name="entity_guid" type="hidden" value="'.$entity_unit->guid.'"/>
        <input name="trackingId" type="hidden" value="'.$entity_unit->guid.'-'.$buyer->guid.'-'.$entity_unit->container_guid.'-'.time().'"/>
        <input name="entity_plugin" type="hidden" value="'.$plugin_name.'"/>
        <input id="prepare_payment" type="submit" name="prepare_payment" class="paypal_button" value="" />  
    </form>

Jquery代码:

$("#prepare_payment").unbind('click').click(function(e) {
    e.preventDefault();     

    var data = {
       action: 'the_php_page_that_will_handle_the_request.php',
       payment_data: $('#prepare_payment_form').serialize()
    };

});

1 个答案:

答案 0 :(得分:0)

我想通了,谢谢。用过这个:

形式:

<form id="'.time().'" action="#" method="post">
        <input name="merchant_id" type="hidden" value="'.$paypal_acount.'"/>
        <input name="merchant_amount" type="hidden" value="'.$entity_price.'"/>
        <input name="site_owner_id" type="hidden" value="'.$site_owner_id.'"/>
        <input name="site_owner_commission" type="hidden" value="'.$site_owner_commission.'"/>
        <input name="currency_code" type="hidden" value="'.$entity_unit->currency.'"/>
        <input name="use_sandbox" type="hidden" value="'.$use_sandbox.'"/>
        <input name="entity_guid" type="hidden" value="'.$entity_unit->guid.'"/>
        <input name="trackingId" type="hidden" value="'.$entity_unit->guid.'-'.$buyer->guid.'-'.$entity_unit->container_guid.'-'.time().'"/>
        <input name="entity_plugin" type="hidden" value="'.$plugin_name.'"/>
        <input id="prepare_payment" type="submit" name="prepare_payment" class="paypal_button" value="" />  
    </form>

Jquery的:

$("form").submit(function (e) {
    e.preventDefault();

    var data = {
       action: 'the_php_page_that_will_handle_the_request.php',
       payment_data: $(this).serialize()
    };
});