2013-02-14 85 views
0

我有一個頁面上有兩個文本框和一個提交按鈕的窗體。我想在短時間內做的事情是讓按鈕和文本字段都像鏈接到同一事物一樣。當他們中的任何一個被點擊時,我想要出現一個「是/否」對話框,其中顯示「正在維護中的表單」,您正在被重定向到這樣的頁面,繼續?否關閉並不執行任何操作,是將您發送到URL。如何使用jQuery將表單輸入字段用作鏈接?

這可能嗎?如果是這樣,用vanilla js或jQuery怎麼能實現呢?

+0

是否真的有理由向他們展示它們的形式?你不能只重定向他們,或者只是有一個錯誤風格類型的頁面? – 2013-02-14 02:18:11

回答

4

您可以將處理程序附加到所有這些輸入字段上的單擊事件。然後,您可以選擇確認功能來提示用戶並在用戶選擇「確定」時重定向用戶。

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"; 
     } 
    }); 
+0

或者只是將點擊附加到整個身體上,或者..在整個表單上放置一個隱形div – 2013-02-14 02:19:00

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"); 
}); 
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 = "/..."; 
    } 
    } 
}); 
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 
    } 
}); 
相關問題