如何给文本框运行时间赋予不同的ID? [英] How to give different id to textbox run time?

查看:101
本文介绍了如何给文本框运行时间赋予不同的ID?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只使用了一个文本框,并将其放入循环中,这样它将创建多个tetxbox.

I have used only single textbox and put it in loop so it will create multiple tetxbox.

我从servlet接收数据,即项目列表(购物车项目)并将其显示在jsp页面上.

I a receiving data from servlet i.e. list of items(cart items) and displaying it on jsp page.

JSP代码:

<% 

        List<Product> prodList = (List<Product>)request.getAttribute("productList");        
        Float totalbill = 0.0f;
%>

<% 
        for(Product p : prodList)
        {           
                    %>
            <tr >
              <td width="226" rowspan="3" ><img src="product_images/<%=p.getpImage() %>" width="100px" height="100px" alt="No Image" /></td>
              <td colspan="7" ><%= p.getpName() %><a onclick="removeProductById(<%= p.getpId()%>);" title="Remove From Cart" > <img alt="" src="images/delete.png" height="25px" width="25px"></a></td>
              </tr>
            <tr style="height: 28px;">
              <td colspan="6" ><div align="right"><%=p.getpPrice() %>0 &nbsp;&nbsp;&nbsp; x &nbsp;&nbsp;&nbsp; </div></td> 
              <td style="border-left:0px;"> <input type="text" value="<%=p.getQty() %>"  id="txtqty" name="txtqty"><a onclick="updateProductById(<%= p.getpId() %>,updateqty());" href="#"  title="Update Quantity"> <img alt="Updt" src="images/updatecart.png" height="25px" width="25px"> </a></td>
              </tr>
            <tr style="height: 28px;">
              <td colspan="7" style="padding-right: 10px;font-size:15px;"><div align="center">Sub Total : &nbsp;&nbsp;&nbsp;<%=p.getQty() * p.getpPrice() %>0</div></td>

              <% totalbill= totalbill +  p.getQty() * p.getpPrice();%>
              </tr>             
            <%                      
        }
%>

因此,这将创建动态数据并显示在页面上. 我在该页面上使用了2个函数来调用servlet并设置数量:

so this will create dynamic data and display on page. I have used 2 functions on that page to call servlet and set quantity :

function updateqty()
    {
        var qty = document.getElementById("txtqty").value;
        return qty;
    }

    function updateProductById(pId,pqty)
    {
        var qty = document.getElementById("txtqty").value;
        $.getJSON("updatecart?pid=" + pId + "&minqty="+ pqty +"&rand=" + Math.floor((Math.random()*100)+1), function(json) {
            alert(json.msg);
            window.location.reload();
        });
    }

因此,当updateqty()函数调用时,它采用第一个文本框的值并将其传递给servlet,但我希望它仅采用被单击的tetxbox的值. 简而言之,无论我单击最后一个项目tetxbox还是任何中间项目,它都需要第一个tetxbox值.

so when updateqty() function calls it takes value of first textbox and pass it to servlet but I want that it will take value of that only tetxbox which is clicked. In short it takes first tetxbox value whether I click on last item tetxbox or any middle.

页面截图

所以有什么帮助吗...

So any help Please...

推荐答案

当您生成id时,它们就像var tid ="T" + i;一样动态.和@锚定点击在参数中使用相同的ID-updateProductById(<%= p.getpId()%>,updateqty(tid));

When u generating id's made them dynamic like var tid = "T"+i; and @ anchor click use same id in parameter - updateProductById(<%= p.getpId() %>,updateqty(tid));

function updateqty(id)
{
    var qty = document.getElementById(id).value;
   return qty;
}

在您添加id和func的JSP循环中执行此操作..

At the JSP loop where you adding the id and func do this ..

<% 
    String id ="T";int i=0;
    for(Product p : prodList)
    {           
                %>
        <tr >
          <td width="226" rowspan="3" ><img src="product_images/<%=p.getpImage() %>" width="100px" height="100px" alt="No Image" /></td>
          <td colspan="7" ><%= p.getpName() %><a onclick="removeProductById(<%= p.getpId()%>);" title="Remove From Cart" > <img alt="" src="images/delete.png" height="25px" width="25px"></a></td>
          </tr>
        <tr style="height: 28px;">
          <td colspan="6" ><div align="right"><%=p.getpPrice() %>0 &nbsp;&nbsp;&nbsp; x &nbsp;&nbsp;&nbsp; </div></td> 
          <td style="border-left:0px;"> <input type="text" value="<%=p.getQty() %>"  id="<%=id+i%>" name="txtqty"><a onclick="updateProductById(<%= p.getpId() %>,updateqty('<%=id+i%>'));" href="#"  title="Update Quantity"> <img alt="Updt" src="images/updatecart.png" height="25px" width="25px"> </a></td>
          </tr>
        <tr style="height: 28px;">
          <td colspan="7" style="padding-right: 10px;font-size:15px;"><div align="center">Sub Total : &nbsp;&nbsp;<%=p.getQty() * p.getpPrice() %>0</div></td>

          <% totalbill= totalbill +  p.getQty() * p.getpPrice();%>
          </tr>             
        <% 
        i++;                     
    }
%>

这篇关于如何给文本框运行时间赋予不同的ID?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆