如何使用jQuery使表单输入字段充当链接?

时间:2013-02-14 02:08:30

标签: javascript jquery forms

我在页面上有一个表单,其中包含两个文本字段和一个提交按钮。我想在短时间内做的是使按钮和文本字段都像是同一个东西的链接。当点击任何一个时,我想要一个Yes / No对话框,显示“正在维护的表格。你被重定向到这样的页面。继续?”没有关闭,什么都不做,是的,您将发送到URL。

这可能吗?如果是这样,怎么能用vanilla js或jQuery实现呢?

4 个答案:

答案 0 :(得分:4)

您可以在所有这些输入字段上附加处理程序到click事件。然后,您可以使用确认功能提示用户,并在用户选择“确定”时重定向用户。

HTML:

<input type="text" />
<input type="text" />
<input type="submit" />

jQuery的:

   $("input").click(function(){
       if(window.confirm("Form under maintenance. You're being redirected to such and such page. Proceed?")){
           location.href = "http://example.com/temp_page";
       }
    });

答案 1 :(得分:0)

你可以在jquery中只在输入上附加一个click事件监听器: http://jsfiddle.net/CuWHH/

<input type='text' id='fred' value='none'>
$("#fred").on("click",function()
{
   //can add a confirm box that redirects to new page here
   window.alert("going fishing");
});

答案 2 :(得分:0)

$('input[type=submit], input[type=text]').click(function() {
    var r=confirm("Form under maintenance. You're being redirected to such and such page. Proceed?");
    if (r==true)
     {
        window.location = "/...";
     }
  }
});

答案 3 :(得分:0)

试试这个jQuery代码:

$("button, input").click(function(e){
    e.preventDefault(); //Stops the default action of an element from happening
    if(confirm("Form under maintenance. You're being redirected to such and such page. Proceed?")) {
        location.href = "http://www.google.com"; //Change it to your URL
    }
});