2014-03-24 25 views
0

我在我的JSP表單上有一個按鈕,它必須發送一個布爾值給控制器。如何添加JavaScript處理程序到按鈕?

<button type="button" class="btn btn-success"><i class="glyphicon glyphicon-ok-circle"></i> Activate</button> 

在窗體上我使用POJO對象,它實際上有一個狀態(布爾狀態)。

public class User implements Serializable { 
    private String name; 
    private boolean status; 
    // getters and setters omitted 
} 

我需要將JavaScript處理程序添加到按鈕,才能將此狀態和用戶標識僅通過POST方法發送到控制器。我怎樣才能使用JavaScript?

UPD:

<c:forEach items="${users}" var="user"> 
    <tr> 
     <td>${user.id}</td> 
     <td>${user.name}</td> 
     <td>${user.email}</td> 
     <td>${user.country}</td> 
     <td class="col-sm-1 col-md-1"> 
      <a href="/user/${user.id}" style="text-decoration: none"> 
       <button type="button" class="btn btn-primary btn-block"><i 
         class="glyphicon glyphicon-pencil"></i> Edit 
       </button> 
      </a> 
     </td> 
     <c:choose> 
      <c:when test="${user.active == true}"> 
       <td class="col-sm-1 col-md-1"> 
        <button type="button" class="btn btn-danger btn-block"><i 
          class="glyphicon glyphicon-remove"></i> Suspend 
        </button> 
       </td> 
      </c:when> 
      <c:otherwise> 
       <td class="col-sm-1 col-md-1"> 
        <button type="button" class="btn btn-success"><i 
          class="glyphicon glyphicon-ok-circle"></i> Activate 
        </button> 
       </td> 
      </c:otherwise> 
     </c:choose> 
    </tr> 
</c:forEach> 
+0

你能分享表格代碼嗎? –

+0

我更新了我的問題。 – user3279337

回答

0

你可以使用如:

function doSend(){ 

    document.forms[0].action = "url?val=true"; 
    document.forms[0].submit(); 

} 

<button type="button" onclick="doSend()" and other attrubute> 

指定您的窗體上的方法屬性。

<form name="myform" method="POST"> 

它會導致表單被提交爲POST。

,也可以使用隱藏域爲做到這一點:

<html> 
<head> 
<script type="text/javascript"> 
function doSend(){ 
    alert("hi"); 
    document.forms[0].setAttribute("method","POST"); 
    document.forms[0].submit(); 
} 
</script> 
</head> 

<body> 
<form action="test2.html" method="GET"> 

    <input type="hidden" name="hiddenField" value="ture1"/> 
    <input type="button" onclick="doSend()" value="click"/> 
</form> 
</body> 
</html> 
+0

您的函數使用GET而不是POST。 – user3279337

+0

您的表單是否有method =「POST」..? –

+0

不,但我想讓我的按鈕發送POST。 – user3279337

0

它使用AJAX ..

function ajaxRequest(){ 
var activexmodes=["Msxml2.XMLHTTP", "Microsoft.XMLHTTP"] //activeX versions to check for in IE 
if (window.ActiveXObject){ //Test for support for ActiveXObject in IE first (as XMLHttpRequest in IE7 is broken) 
    for (var i=0; i<activexmodes.length; i++){ 
    try{ 
    return new ActiveXObject(activexmodes[i]) 
    } 
    catch(e){ 
    //suppress error 
    } 
    } 
} 
else if (window.XMLHttpRequest) // if Mozilla, Safari etc 
    return new XMLHttpRequest() 
else 
    return false 
} 

跟着你必須在click事件調用函數。

function callOnClick(usrid) 
    { 
    var mypostrequest=new ajaxRequest() 
     mypostrequest.onreadystatechange=function(){ 
     if (mypostrequest.readyState==4){ 
      if (mypostrequest.status==200 || window.location.href.indexOf("http")==-1){ 
      //action todo after successful return 
      } 
      else{ 
      alert("An error has occured making the request") 
      } 
     } 
     } 
     var status=encodeURIComponent(document.getElementById("status").value)// make a hidden field and set the status in value attribute 
     var userid=usrid//set this value as the attribute of javascript function 
     var parameters="status="+status+"&id="+userid 
     mypostrequest.open("POST", "name of controller or servlet", true) 
     mypostrequest.setRequestHeader("Content-type", "application/x-www-form-urlencoded") 
     mypostrequest.send(parameters) 
    } 
+0

未捕獲的類型錯誤:無法讀取null的屬性值' – user3279337

+0

請參閱註釋文本「創建隱藏字段並在值屬性中設置狀態」 –

+0

「> –

相關問題