简体   繁体   English

使用JSTL动态更改文本字段值

[英]Dynamically changing text field value using JSTL

I'm a beginner at JSTL as I've only started using it recently to avoid using scriplets in my JSP, and I wanted to ask for help about something I've been stuck in. 我是JSTL的初学者,因为我最近才开始使用它,以避免在我的JSP中使用scriplets,所以我想寻求有关我被困的东西的帮助。

I have a select element which I've managed to populate with an arraylist using JSTL. 我有一个选择元素,我已经设法使用JSTL用arraylist填充它。 However I also need to dynamically change the value of a readonly text field whenever a different option in the dropdown list is selected, and the value to put in the text field is at the same index in the arraylist as the selected option, and that's where I'm having trouble with. 但是,每当在下拉列表中选择了其他选项时,我还需要动态更改只读文本字段的值,并且要在文本字段中放置的值与选定选项在数组列表中的索引相同,这就是我有麻烦了。

<select id="department" onchange="managerfill()">
<c:forEach var="dept" items="${departments}">
<option value="${dept.deptname}"><c:out value="${dept.deptname}"/></option>
</c:forEach>
</select>

<input type="text" id="manager" readonly>

I tried to use a js function to change the text field's value but so far, I've had no luck with getting it to work. 我试图使用js函数来更改文本字段的值,但到目前为止,我还没有让它起作用。

function managerfill() {
var x = document.getElementById("department").selectedIndex;
document.getElementById("manager").value="${departments[x].manager}";
}

It would have been easier if both department and manager fields were both dropdown lists, but we were required to make manager a text field so I'm kind of at a loss trying to work around it. 如果部门和经理字段都为下拉列表,这会更容易,但是我们需要将经理设为文本字段,因此尝试解决此问题我有点无所适从。

You can't use jstl language on rendered pages. 您不能在呈现的页面上使用jstl语言。 jstl is server side only and can't be used on end-user browser. jstl仅用于服务器端,不能在最终用户浏览器上使用。

You need to use js to change it. 您需要使用js进行更改。

The function you have wrote is good expect the jstl syntax that cannot be no more interpreted. 您编写的函数很好地期望了无法再解释的jstl语法。

function managerfill() {
  var select = document.getElementById("department");
  var x = select.selectedIndex;
  document.getElementById("manager").value = select.options[x].text;
}

Something like this should work. 这样的事情应该起作用。

But I think that the text you want to print is server side, so you can print it in some html/tag or hidden select and take the result from it. 但是我认为您要打印的文本是服务器端的,因此您可以将其打印为html / tag或隐藏的select并从中获取结果。 replacing the select.options[x].text : 替换select.options[x].text

<select id="departments-manager" class="hidden">
  <c:forEach var="dept" items="${departments}">
    <option value="${dept.manager}"><c:out value="${dept.manager}"/></option>
  </c:forEach>
</select>


function managerfill() {
  var select = document.getElementById("department");
  var selectManager = document.getElementById("departments-manager");
  var x = select.selectedIndex;
  document.getElementById("manager").value = selectManager.options[x].text;
}

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

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