2015-09-11 23 views
-2

我想在onsubmit上添加啓用和禁用功能,或相應地禁用提交按鈕。onsubmit上的帖子值以及啓用和禁用提交按鈕

當我點擊啓用按鈕時,它應該是不可點擊(禁用)和禁用按鈕應該是可點擊(啓用),當我點擊禁用按鈕的另一種方式 - 啓用和禁用功能工作正常,但我面對一個問題:當我發佈print_r($ _ POST)的值時,不會顯示任何內容。爲什麼?

JS

$('input').click(function() { 
    var id = $(this).get(); 
    toggleButtons(id); 
}); 
function toggleButtons(id){ 
    // Check for attr != disabled here 
    $('input').removeAttr("disabled"); 
    $(id).attr("disabled","disabled"); 
}   

HTML

<?php print_R($_POST); ?> 
<!DOCTYPE html> 
<html> 
    <head>  
    <script src="code.jquery.com/jquery-latest.js"></script>; 
    </head> 
    <body> 
    <form method="post" action=""> 
     <input type="submit" value="Button1" disabled="disabled" name="testing" id="testing"/> 
     <input type="submit" value="Button2" name="test" id="test"/> 
    </form> 
    </body> 
</html> 
+0

告訴我們你做了什麼,我們一定會幫你... – Lal

+0

這是HTML代碼:<?PHP \t的print_r($ _POST); ?> <!DOCTYPE HTML> \t \t \t \t \t \t \t \t <形式方法=」 POST 「行動= 」「> \t \t \t \t \t \t \t \t \t –

+0

不要添加它作爲評論..請編輯問題並粘貼HTML .. – Lal

回答

1

我會用JavaScript來添加CSS類 '按鈕禁用',以它的提交,這將是這樣的:

CSS

.button-disabled { 
    pointer-events: none; 
} 

的JavaScript

$('input').click(function() { 
    var id = $(this).get(); 
    $(this).addClass('button-disabled'); 
    // do other logic here 
}); 

不知道這是你要找的是什麼,但是這是一個前端解決方案。

+0

如果頁面在提交時實際上被卸載,它將不可見 – mplungjan

0
  1. 你不能看到殘疾人按鈕,如果您提交的形式,因爲該頁面被卸載
  2. 您不能禁用提交按鈕,因爲它會停止提交。

你可以做的是處理提交事件和後它

您也可以,如果你想實際提交表單,並停留在頁面上使用的iframe或新窗口的目標。否則,使用AJAX

所以

DEMO

$(function(e) { 
    $("form").on("submit",function(e) { 
     e.preventDefault(); // cancel actual submit 
     // ajax the form 
     $.post(this.action,$(this).serialize(),function() { 
      alert("submitted"); 
     });  

    }); 
    $('input').click(function() { 
     var id = $(this).get(); 
     toggleButtons(id); 
    }); 
    function toggleButtons(id){ 
     var but = id; 
//  setTimeout(function() { // uncomment if you want to submit to new frame/window 
      console.log("dis") 
      // Check for attr != disabled here 
      $('input').removeAttr("disabled"); 
      $(id).attr("disabled","disabled"); 
//  },10); // uncomment if frame/window as taget 
    }   
});