weixin_33724059 2015-03-31 19:28 采纳率: 0%
浏览 16

带图像按钮的AJAX

I have tried different combinations of input type but how am I able to input a message without messing up the image? The purpose was to update the table without refreshing the page. To be more specific, when I clicked the image it should update the table on the side of the page. For many of the tutorial out there I saw people only use for onclick to call the ajax functions. Is there a good example for what I can do for the image button instead of the plain button?

For example:

<form name="bakery" action="TestMartController" method="post" >
    <input type="hidden" name="action" value="dairy">
    <input type="image" src="<%=request.getContextPath()%>/css/categories/dairy.jpg" onclick="loadXMLDoc">

The table I want to update is

<div id="refresh">  
    <table class="rightTable" border="1" width="70%">
    <tr>
        <th>Photo</th>
        <th>Product and Description</th>
        <th>Price</th>
        <th>Orders</th>
        <th>Quantity</th>
        <th>Edit Quantity</th>
        <th>Add Item </th>
    </tr>
    <c:forEach var="b" items="${bakery}">
...
    </c:forEach>    
    </table>
</div>

Javascript file

function loadXMLDoc()
{
var xmlhttp;
if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  }
else
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
xmlhttp.onreadystatechange = function() {

    if (xmlhttp.readyState == 4 ) {
       if(xmlhttp.status == 200){
           document.getElementById("refresh").innerHTML = xmlhttp.responseText;
       }
       else if(xmlhttp.status == 400) {
          alert('There was an error 400');
       }
       else {
           alert('something else other than 200 was returned');
       }
    }
};

xmlhttp.open("POST", "bakery.jsp", true);
xmlhttp.send();
}
  • 写回答

1条回答 默认 最新

  • Lotus@ 2015-03-31 19:38
    关注

    First of all you want a ajax call which makes post to a service and get datas and update your table.

    I suggest to you learn how to use jquery and ajax call.

    1- In your html define call method. Don't use form!

    <img src="yourImage.jpg" onclick="getSomeData()">
    

    2- Make post or get call to your service with jquery. And get Data.

    function getSomeData(){
    var url = "http://url";
        $.post(url,{data:data})
        .done(function(data){
           //this data is your call result.
           //do something
           updateTable(data);
        })
        .fail(function(){
            console.log('fail');
        });
    
    } 
    

    3- Create your gui function to change table.

    function updateTable(){
    //update your htmls
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?