简体   繁体   English

如何链接html表单和锚标记

[英]how to Link html form and anchor tag

I am new to HTML. 我是HTML的新手。 I want the following to be done in html. 我希望在html中完成以下操作。

if(link is clicked) {
    process one form tag
}
else {
    some other form tag
}

       this is my form tag . 
        <form name="input" action="abc.pl" method="get" id="sel">
        <input type="checkbox" name="vehicle" value="Bike">I have a bike<br>
         <input type="checkbox" name="vehicle" value="Car">I have a car 
        </form>

I want a link such that if i click on the link the above forms input(i mean the checkboxes value) should be taken and the link should process another .pl file in action ... 我想要一个链接,以便如果我单击该链接,则应采用上述形式输入(我的意思是复选框的值),并且该链接应在操作中处理另一个.pl文件...

I think you could try to change the form action dynamically when user clicked the link by using jQuery. 我认为当用户使用jQuery单击链接时,您可以尝试动态更改表单操作。 Here is a simple example: 这是一个简单的示例:

HTML 的HTML

<form id="form1" action="http://jimmy.right-pet.cc/test.php" method="post">
  <legend>Test</legend>
  <fieldset>
    <label for="name">Name</label>
    <input type="text" name="name" id="name"/>
    <input id="submit-btn" type="submit" value="submit"/>
  </fieldset>
</form>
<a id="link" href="#">change form action link</a>

JS JS

<script>
  $(function(){
    $("#link").click(function(){
      $("#form1").attr("action","http://jimmy.right-pet.cc/test2.php");
    });
  });
</script>

And try to use the browser dev tool to check if the form action is changed after you clicked the link. 并尝试使用浏览器开发工具来检查单击链接后是否更改了表单操作。

Here is a jsFiddle demo . 这是一个jsFiddle演示

Hope it helpful. 希望对您有所帮助。

Not strictly an answer to the question, but still a solution to the problem. 严格来说,这不是问题的答案,但仍然是问题的解决方案。

From the comments: 从评论:

Don't do that. 不要那样做 Submit to one server side URI. 提交到一个服务器端URI。 Do it with submit buttons. 用提交按钮来做。 Look at the value of the submit button in the form data to determine which branch of code to run on the server. 查看表单数据中的Submit按钮的值,以确定要在服务器上运行哪个代码分支。 – Quentin 1 hour ago –昆汀1小时前

@Quentin Please Give me an example . @Quentin请给我一个例子。 I am new to HTML 我是HTML新手

In the HTML. 在HTML中。

<form name="input" action="abc.pl" method="get" id="sel">
    <label>
        <input type="checkbox" name="vehicle" value="Bike">
        I have a bike
    </label>

    <label>
        <input type="checkbox" name="vehicle" value="Car">
        I have a car 
    </label>

    <input type="submit" name="sub" value="Some Action">
    <input type="submit" name="sub" value="Some Other Action">
</form>

And then in abc.pl : 然后在abc.pl

use strict;
use warnings;
use CGI;

my $c = CGI->new;
my $sub = $c->param('sub');

unless ($c) {
    # No submit value was detected so either perform a default
    # action or return an error
    exit;
}

if ($c eq "Some Action") {
    # Do one thing
} elseif  ($c eq "Some Other Action") {
    # Do another thing
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM