从链接中捕获ID,然后将其传递给弹出窗体

时间:2017-01-30 22:00:11

标签: javascript forms parameter-passing

我有三个链接都具有相同的类但ID不同。单击带有类名的链接时,用户会看到一个弹出窗体。我尝试做的是从点击中捕获个人ID,并将其作为隐藏字段传递给表单。

/*======== This is the popup form which is show to the user when they click the "Get a Quote." ========*/
<form id="addProdGateway" action="" method="post">
<input type="hidden" readonly="readonly" id="product" name="product" value="">
</form>

我希望能够在捕获后将值添加到弹出窗口中的以下隐藏字段:

jQuery(document).on('submit','#addProdGateway',function(e) {
    e.preventDefault();
    validationAddProdGateway();

    /*======== AJAX to submit a Pardot Form Handler ========*/
}

function validationAddProdGateway()
{
    var x = 0;
    var theForm = document.forms["addProdGateway"];
    var email = theForm.elements["email"].value;
    var source = theForm.elements["source"].value;
    var product = theForm.elements["product"].value;
    var url;

    if (product = "IA")
    {
        url = "https://www.ia.com"
    }
    else if (product = "MP")
    {
        url = "https://www.mp.com"
    }
    else if (product = "GS")
    {
        url = "https://www.gs.com"
    }

    /*======== Form Validation ========*/

    window.location.href = url;
}

根据表单上的ID,用户将被重定向到正确的站点。我已经构建了js的后半部分,但似乎无法弄清楚如何捕获该ID并在提交之前将其传递到表单中。

我已经在下面构建了验证和处理:

SUMIFS

1 个答案:

答案 0 :(得分:1)

下面的代码段显示了如何使用jQuery获取点击元素的父ID:

&#13;
&#13;
$('.addProdClick').click(function(el) {
  $('#product').val($(this).parent().attr('id'));  
});
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="IA"><a class="addProdClick">Get a Quote</a></div>
<div id="MP"><a class="addProdClick">Get a Quote</a></div>
<div id="GS"><a class="addProdClick">Get a Quote</a></div>

<input type="hidden" readonly="readonly" id="product" name="product" value="">
&#13;
&#13;
&#13;