简体   繁体   English

使用JQuery根据组合框选择更新文本字段-访问列表问题

[英]Update textfield based on combobox selection using JQuery - accessing list problem

I have a Spring MVC application, use JSP+JQuery for view, and what I need is to, based on combo box selection (which gets me an index of element in list) to populate text field. 我有一个Spring MVC应用程序,使用JSP + JQuery进行查看,我需要的是基于组合框选择(这使我获得了列表中元素的索引)来填充文本字段。

listProduct - list of products that is in the model listProduct-模型中的产品列表

<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>

My question is: When i put number in listProduct[] ie listProduct[0] it works just fine and price field gets populated, but when i want put "comboIndex" in brackets, it does nothing. 我的问题是:当我将数字放在listProduct []即listProduct [0]中时,它工作得很好,并且价格字段也已填充,但是当我想将“ comboIndex”放在方括号中时,它什么也不做。

If there is another solution (not using JQuery), please post it 如果还有其他解决方案(不使用JQuery),请发布它

You are mixing client and server side code. 您正在混合客户端和服务器端代码。 You cannot pass a JS variable to your server code, as your server code is parsed before the page has been served up to the client. 您无法将JS变量传递给服务器代码,因为在将页面提供给客户端之前就已经解析了服务器代码。

You will need to pass the price corresponding to a particular product ID to the client by other means. 您将需要通过其他方式将与特定产品ID相对应的价格传递给客户。

The relevant code (probably in a change event handler ) should reference productCombobox selectedIndex instead: 相关代码(可能在更改事件处理程序中 )应改为引用productCombobox selectedIndex

 var comboIndex = $("#productCombobox").attr("selectedIndex"); //
 $("#priceInput").val(${listProduct[comboIndex].price});     

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

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