从html输入字段获取值

从html输入字段获取值,html,jsp,jstl,Html,Jsp,Jstl,如何使用id=“new\u amount”获取输入值,并将其正确放入更改金额url路径。我现在尝试的显然是不正确的: <td> <input type="button" onclick="location.href='${pageContext.request.contextPath}/update?id=${object.id}&?amount=${document.getElementById("new_amount").value}';" value=

如何使用
id=“new\u amount”
获取输入值,并将其正确放入更改金额url路径。我现在尝试的显然是不正确的:

<td>
     <input type="button"
onclick="location.href='${pageContext.request.contextPath}/update?id=${object.id}&?amount=${document.getElementById("new_amount").value}';" 
value="Change Amount" />
</td>

谁能帮帮我吗

<table class=goods>
<tr>
    <td>Name</td>
    <td>Amount</td>
</tr>
<c:forEach var="object" items="${goods}">
    <tr>
        <td id="name"><c:out value="${object.name}"></c:out></td>
        <td id="amount"><c:out value="${object.amount}"></c:out></td>
        <td id="new_amount"><p>New amount:</p> <input type="number" id="new_amount" /></td>
        <td><input type="button"
            onclick="location.href='${pageContext.request.contextPath}/update?id=${object.id}&?amount=${document.getElementById("new_amount").value}';" value="Change Amount" /></td>
        <td><input type="button" onclick="location.href='${pageContext.request.contextPath}/delete?id=${object.id }';" value="Delete"/></td>
    </tr>
</c:forEach>
<tr>
    <td><a href="${pageContext.request.contextPath }/addgoods">Add new
        goods</a></td>
</tr>

名称
数量
新金额:


我对三行进行了一些静态变通(用于循环)。这将对你有帮助

为表指定id,而不是直接在调用函数时附加值,并为
对象\u id
使用隐藏值。还向元素添加了类,以发现(
ID
对于重复的元素不是个好主意)

您只需使用
JSTL
代码更改静态值

<table id="goods">
    <tr>
        <th>Name</th>
        <th>Amount</th>
        <th>New Amount</th>
        <th>Actions</th>
    </tr>
    <tr>
        <td>test1</td>
        <td>100</td>
        <td><input type="number" name="new_amount" class="amount" /></td>
        <td><input type="button" onclick="updateObject(this)" value="Edit" /> / <input type="button" onclick="deleteObject(this)" value="Delete"/></td>
        <input type="hidden" value="1" class="goods_id" />
    </tr>
    <tr>
        <td>test2</td>
        <td>200</td>
        <td><input type="number" name="new_amount" class="amount" /></td>
        <td><input type="button" onclick="updateObject(this)" value="Edit" /> / <input type="button" onclick="deleteObject(this)" value="Delete"/></td>
        <input type="hidden" value="2" class="goods_id" />
    </tr>
  <tr>
        <td>test3</td>
        <td>300</td>
        <td><input type="number" name="new_amount" class="amount" /></td>
        <td><input type="button" onclick="updateObject(this)" value="Edit" /> / <input type="button" onclick="deleteObject(this)" value="Delete" /></td>
        <input type="hidden" value="3" class="goods_id" />
    </tr>
</table>
看。如果你需要,我可以解释更多。希望这有帮助

更新:

在HTML中, 您的实际需求如下所示,我使用
JSTL

<table id="goods">
<tr>
    <td>Name</td>
    <td>Amount</td>
    <td>New Amount</td>
    <td>Actions</td>
</tr>
<c:forEach var="object" items="${goods}">
    <tr>
        <td><c:out value="${object.name}"></c:out></td>
        <td><c:out value="${object.amount}"></c:out></td>
        <td><p>New amount:</p> <input type="number" id="new_amount" class="amount" /></td>
        <td><input type="button" onclick="updateObject(this)" value="Change Amount" /> / 
            <input type="button" onclick="deleteObject(this)" value="Delete"/></td>
        <input type="hidden" value="${object.id}" class="goods_id" />
    </tr>
</c:forEach>
<tr>
    <td><a href="${pageContext.request.contextPath }/addgoods">Add new
        goods</a></td>
</tr>

我已经用三行做了一些静态变通(用于循环)。这将对你有帮助

为表指定id,而不是直接在调用函数时附加值,并为
对象\u id
使用隐藏值。还向元素添加了类,以发现(
ID
对于重复的元素不是个好主意)

您只需使用
JSTL
代码更改静态值

<table id="goods">
    <tr>
        <th>Name</th>
        <th>Amount</th>
        <th>New Amount</th>
        <th>Actions</th>
    </tr>
    <tr>
        <td>test1</td>
        <td>100</td>
        <td><input type="number" name="new_amount" class="amount" /></td>
        <td><input type="button" onclick="updateObject(this)" value="Edit" /> / <input type="button" onclick="deleteObject(this)" value="Delete"/></td>
        <input type="hidden" value="1" class="goods_id" />
    </tr>
    <tr>
        <td>test2</td>
        <td>200</td>
        <td><input type="number" name="new_amount" class="amount" /></td>
        <td><input type="button" onclick="updateObject(this)" value="Edit" /> / <input type="button" onclick="deleteObject(this)" value="Delete"/></td>
        <input type="hidden" value="2" class="goods_id" />
    </tr>
  <tr>
        <td>test3</td>
        <td>300</td>
        <td><input type="number" name="new_amount" class="amount" /></td>
        <td><input type="button" onclick="updateObject(this)" value="Edit" /> / <input type="button" onclick="deleteObject(this)" value="Delete" /></td>
        <input type="hidden" value="3" class="goods_id" />
    </tr>
</table>
看。如果你需要,我可以解释更多。希望这有帮助

更新:

在HTML中, 您的实际需求如下所示,我使用
JSTL

<table id="goods">
<tr>
    <td>Name</td>
    <td>Amount</td>
    <td>New Amount</td>
    <td>Actions</td>
</tr>
<c:forEach var="object" items="${goods}">
    <tr>
        <td><c:out value="${object.name}"></c:out></td>
        <td><c:out value="${object.amount}"></c:out></td>
        <td><p>New amount:</p> <input type="number" id="new_amount" class="amount" /></td>
        <td><input type="button" onclick="updateObject(this)" value="Change Amount" /> / 
            <input type="button" onclick="deleteObject(this)" value="Delete"/></td>
        <input type="hidden" value="${object.id}" class="goods_id" />
    </tr>
</c:forEach>
<tr>
    <td><a href="${pageContext.request.contextPath }/addgoods">Add new
        goods</a></td>
</tr>

是的,不正确。文本框位于循环内部,您不能对所有元素使用相同的
id
。您有
。您希望
document.getElementById(“新金额”)
返回哪一个?是的,它不正确。文本框位于循环内部,您不能对所有元素使用相同的
id
。您有
。您希望
document.getElementById(“新金额”)
返回哪一个?