简体   繁体   English

使用JavaScript更改struts2迭代器内的输入值

[英]Change value of input inside an struts2 iterator using javascript

I need to change the value of an input type text which is inside an iterator of struts2. 我需要更改在struts2的迭代器内的输入类型文本的值。 I don't like to refresh all the page, I like to refresh the value of the input only and I think that using JSON could be a solution (I'm not sure if this is the best way, I'm junior programmer...) 我不喜欢刷新所有页面,我只想刷新输入的值,而且我认为使用JSON可能是一种解决方案(我不确定这是否是最好的方法,我是初级程序员。 ..)

This is my jsp 这是我的jsp

<input type="text" name="cantidad" id="cantidadIndividual" readonly="readonly" value="<s:property value="#a.cestaUnidades"/>">
    <img src="../Imagenes/Administracion/Signo_Mas.png" id="mas" onclick="MasMenosCantidad('+',<s:property value="#a.cestaUnidades"/>,<s:property value="#a.cestaId"/>,<s:property value="#a.ropaStock.rostockUnidades"/>);"/>

    <script>
            function MasMenosCantidad(valor,cantidad,id,stock){
                document.getElementById("clave").value = id;
                if(valor == '+'){
                    cantidad++;
                }
                if(valor == '-'){
                    cantidad--;
                }
                if(cantidad <= stock){
                    document.getElementById("cantidadIndividual").value = cantidad;
                    usarAJAXCantidad(id,cantidad);
                    //document.getElementById("formCantidad").submit();
                } else {                    
                    if(valor == '-'){
                        document.getElementById("cantidadIndividual").value = stock;
                        usarAJAXCantidad(id,cantidad);
//                        document.getElementById("formCantidad").submit();
                    } else {
                        alert("Stock excedido");
                        }
                    }
                }
            }
            function usarAJAXCantidad(clave,cant){
                $.getJSON('ajaxCantidad', {
                    clave : clave,
                    cantidad : cant
                }, function(jsonResponse3) { 
                    var nuevaCantidad = $('#cantidadIndividual');
                    $.each(jsonResponse3.stateMap3, function(key, value) {
                        nuevaCantidad.value = value;
                    });
                });
            }            
        </script>

The strutx.xml strutx.xml

<action name="ajaxCantidad" class="Acciones.HomeCesta" method="ajaxCantidad">
            <result type="json">
                <param name="excludeNullProperties">true</param>
                <param name="noCache">true</param>
            </result>
        </action>

HomeCesta.java HomeCesta.java

public String ajaxCantidad() throws Exception {

        c = ControladoresDAO.cCesta.RecuperaPorId(clave);
        c.setCestaUnidades(cantidad);
        ControladoresDAO.cCesta.Modifica(c);
        stateMap3.clear();
        stateMap3.put("", ""+cantidad);
        return SUCCESS;

    }

My problem is that in the iterator, only the first input with id cantidadIndividual is refreshed, but if I made click in a second, third... button with image Signo_Mas.png, the value is showing in the first input with id cantidadIndividual. 我的问题是,在迭代器中,只有ID为cantidadIndividual的第一个输入才被刷新,但是如果我单击具有图像Signo_Mas.png的第二个,第三个...按钮,则该值将显示在ID为cantidadIndividual的第一个输入中。

When you use document.getElementById("cantidadIndividual") only the first element with the same id is returned. 当您使用document.getElementById("cantidadIndividual")仅返回具有相同ID的第一个元素。 You should change ids to be unique for each iteration. 您应将ID更改为每次迭代唯一。

You can use a variable in JSP like this 您可以像这样在JSP中使用变量

document.getElementById("cantidadIndividual${uniqueIdentifier}")

or if you are using Struts2 或者如果您正在使用Struts2

document.getElementById("cantidadIndividual<s:property value='%{uniqueIdentifier}'/>")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM