Javascript 使用JQuery根据组合框选择更新textfield-访问列表问题
我有一个SpringMVC应用程序,使用JSP+JQuery进行查看,我需要的是,基于组合框选择(它为列表中的元素获取索引)填充文本字段 listProduct—模型中的产品列表Javascript 使用JQuery根据组合框选择更新textfield-访问列表问题,javascript,jquery,jsp,spring-mvc,Javascript,Jquery,Jsp,Spring Mvc,我有一个SpringMVC应用程序,使用JSP+JQuery进行查看,我需要的是,基于组合框选择(它为列表中的元素获取索引)填充文本字段 listProduct—模型中的产品列表 <form:form method="POST" commandName="productForm" name="insertRacun"> <table> <tr> <td class="ui-widget">Product:</td> <
<form:form method="POST" commandName="productForm" name="insertRacun">
<table>
<tr>
<td class="ui-widget">Product:</td>
<td><form:select path="productListId" id="productCombobox">
<form:options items="${listProduct}"itemLabel="name" itemValue="productId"/>
</form:select>
</td>
<td class="ui-widget">Product price:</td>
<td><form:input path="priceList"
class="ui-widget ui-widget-content" id="priceInput" />
</td>
<script type="text/javascript">
var comboIndex = $("#productCombobox").val();
$("#priceInput").val(${listProduct[comboIndex].price})
});
</script>
产品:
产品价格:
var comboIndex=$(“#productCombobox”).val();
$(“#priceInput”).val(${listProduct[comboIndex].price})
});
我的问题是:当我把数字放在listProduct[]中,即listProduct[0]时,它工作正常,并且价格字段被填充,但当我想把“comboIndex”放在括号中时,它什么也不做
如果有其他解决方案(不使用JQuery),请发布它相关代码(可能在a中)应参考productCombobox:
相关代码(可能在a中)应参考productCombobox:
您正在混合客户端和服务器端代码。您不能将JS变量传递给服务器代码,因为在将页面提供给客户端之前,服务器代码已被解析
您需要通过其他方式将与特定产品ID对应的价格传递给客户端。您正在混合客户端代码和服务器端代码。您不能将JS变量传递给服务器代码,因为在将页面提供给客户端之前,服务器代码已被解析
您需要通过其他方式将特定产品ID对应的价格传递给客户。artikalCombobox实际上是productCombobox-错误为translation@Ivan请参阅更新,您的代码返回的是选定项的值,而不是其在列表中的索引问题在于listProduct[]中的索引。除了数字外,不能将列表作为索引传递。我试着把“var x=0;listProduct[x]”放进去,但它不起作用,但对于listProduct[0]来说,它起不了作用works@Ivan啊,好吧,我没看到。卡里姆是对的。您需要将对会话属性“${listProduct}”的引用更改为其他客户端。将数据粘贴到javascript数组中,并引用它?artikalCombobox实际上是productCombobox-错误为translation@Ivan请参阅更新,您的代码返回的是选定项的值,而不是其在列表中的索引问题在于listProduct[]中的索引。除了数字外,不能将列表作为索引传递。我试着把“var x=0;listProduct[x]”放进去,但它不起作用,但对于listProduct[0]来说,它起不了作用works@Ivan啊,好吧,我没看到。卡里姆是对的。您需要将对会话属性“${listProduct}”的引用更改为其他客户端。将数据粘贴到javascript数组中并引用它?
var comboIndex = $("#productCombobox").attr("selectedIndex"); //
$("#priceInput").val(${listProduct[comboIndex].price});