link id使用jquery将值传递给表单

时间:2016-10-26 06:43:15

标签: javascript jquery html5

<a href="#" id="customer" class="link">Customer Registation</a>

我已经从链接中获取了linkID,如何将linkID传递给表单操作action=linkID

$(".link").click(function(e){
    e.preventDefault();
    var linkID = $(this).attr("id");
    $("#registationForm").fadeIn(300,function(){$(this).focus();});
});

<div id="registationForm" tabindex="-1">
<form id="registation-form" method="POST" action="registation/register.php?action=linkID">
</div>

谢谢你的帮助。

2 个答案:

答案 0 :(得分:0)

只需准备好action属性即可获取linkID。然后使用jquery,您可以按ID选择form,然后操纵action

$(".link").click(function(e){
    e.preventDefault();
    var linkID = $(this).attr("id");

    //below three lines must be added.
    var $registrationForm = $('#registation-form');
    var actionLink = $registrationForm.attr('action');    
    $registrationForm.attr('action',actionLink + linkID );
    //end

    $("#registationForm").fadeIn(300,function(){$(this).focus();});
});

<div id="registationForm" tabindex="-1">
<form id="registation-form" method="POST" action="registation/register.php?action=">
</div>

如果您找到一个班轮代码,可以尝试下面的内容。

$('#registation-form').attr('action',$('#registation-form').attr('action')+ linkID )

答案 1 :(得分:0)

var linkID,
    $formWrapper = $("#registationFormWrapper"),
    $form        = $("#registationForm");

$(".link").click(function(e) {

  e.preventDefault();

  linkID = this.id;

  $formWrapper.fadeIn(300, function() {
    $form.find("input").focus();
  });

});

$form.on("submit", function() {
  this.action = "registation/register.php?action="+ linkID;
});
#registationFormWrapper {
  display: none;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<a class="link" id="something" href="#">something</a>

<div id="registationFormWrapper">
  <form id="registationForm" method="POST">
    <input type="text">
    <button type="submit">SUBMIT</button>
  </form>
</div>

单击链接,然后单击“提交”。检查控制台中的表单以查看它的action属性值