[英]Display dynamic text boxes upon selecting the particular value from various drop down in the same iteration
I want to display the text box when a particular value from the list is selected .I am able to do so but the thing is i have been iterating a loop to print that drop down of list.The Drop down appear twice.How can i make the text box appear on the specific value selection form both the drop down selection. 我想从列表中选择一个特定的值时显示文本框。我能够这样做,但问题是我一直在循环循环以打印列表的下拉菜单。下拉菜单出现两次。使文本框同时显示在特定的值选择窗体上。
Here is my JSP code 这是我的JSP代码
<s:iterator value="schemePayoutDTO.schemePayoutListFSE" var="fsePay" status="fsePayStatus">
<s:hidden name="schemePayoutDTO.schemePayoutListFSE[%{#fsePayStatus.index}].paramID" />
<tr>
<td>
<s:textfield name="schemePayoutDTO.schemePayoutListFSE[%{#fsePayStatus.index}].paramValue1" label="%{#fsePay.paramName}"></s:textfield>
<s:div id="div1" cssStyle="margin-left:15cm; position: relative; bottom:-45px;">
<s:select style="width: 150px;" id="select" list="schemPayoutTypeList" listKey="payoutType" listValue="payoutTypeVal" name="payoutTypeVal" onchange="showPayoutValue(this.selectedIndex)" />
</s:div>
</td>
</tr>
</s:iterator>
I am using the function showPayoutValue
on select tag of struts to show the text box but that text box is appearing once. 我在struts的select标签上使用function showPayoutValue
来显示文本框,但该文本框只出现一次。 I want to show the text box for every correct value selection from the drop down. 我想从下拉列表中显示每个正确值选择的文本框。
Javascript code JavaScript代码
function showPayoutValue(name){
if(name=='1' )
document.getElementById('div1').innerHTML='Capping: <input type="text" name="other" />';
else
document.getElementById('div1').innerHTML='';
}
Here is the screen shot of what i am doing.Also beside the Usage text box if i am selcting the value Percentage with capping then CApping text box must appear. 这是我正在执行的操作的屏幕截图。如果在“用法”文本框旁边选择了“带上限的百分比”值,则必须出现“ CApping”文本框。
Can someone please help me put for this. 有人可以帮我解决这个问题。
Please suggest me any other way out if any. 如果有其他建议,请提出其他建议。
I will be very thankful 我会很感激
Few things to note 几件事要注意
iterator
will create duplicate id
s which is against the rules of HTML
. 您的iterator
将创建违反HTML
规则的重复id
。 The id
should be unique per page . 该id
每页应该唯一 。 document.getElementById
it will always get the first matching element
and hence the first div1
will have the input
always. 当您说document.getElementById
,它将始终获取第一个匹配element
,因此第一个div1
将始终具有input
。 id
with class
and try to match the element
which is always closest
to the select
element. 更换你的id
与class
并尝试匹配element
这始终是closest
的select
元素。 select
element inside div1
and changing its innerHTML
. 您还将select
元素放置在div1
并更改其innerHTML
。 This will actually, as of my knowledge, will remove the select
element too. 据我所知,这实际上也会删除select
元素。 So place it outside the div1
element, probably in a separate div
因此,请将其放在div1
元素之外,可能放在单独的div
Changes to be made 进行更改
JSP Changes
<s:iterator value="schemePayoutDTO.schemePayoutListFSE" var="fsePay" status="fsePayStatus">
<s:hidden name="schemePayoutDTO.schemePayoutListFSE[%{#fsePayStatus.index}].paramID" />
<tr>
<td>
<s:textfield name="schemePayoutDTO.schemePayoutListFSE[%{#fsePayStatus.index}].paramValue1" label="%{#fsePay.paramName}"></s:textfield>
<s:select style="width: 150px;" class="select" list="schemPayoutTypeList" listKey="payoutType" listValue="payoutTypeVal" name="payoutTypeVal" onchange="showPayoutValue(this)" />
<!-- Pass element instead of value to identify which element has to targeted -->
<!-- Keep this outside-->
<s:div class="div1" cssStyle="margin-left:15cm; position: relative; bottom:-45px;">
<!--Change id to class and keep this empty-->
</s:div>
</td>
</tr>
</s:iterator>
JS Changes
function showPayoutValue(ctrl) {
var name = ctrl.value;//get the selected value
var targetDiv=ctrl.parentElement.getElementsByClassName('div1')[0];
//get Target div relating to its parent
if (name == '1')
targetDiv.innerHTML = 'Capping: <input type="text" name="other" />';
else
targetDiv.innerHTML = '';
//You can also use conditional operators instead of if else here as below
//targetDiv.innerHTML=name==1?'Capping: <input type="text" name="other" />':'';
}
DEMO
Assuming html
will be generated as what given in DEMO
假设将按照DEMO
说明生成html
function showPayoutValue(ctrl) { var name = ctrl.value; var targetDiv=ctrl.parentElement.getElementsByClassName('div1')[0]; if (name == '1') targetDiv.innerHTML = 'Capping: <input type="text" name="other" />'; else targetDiv.innerHTML = ''; }
<div value="schemePayoutDTO.schemePayoutListFSE" var="fsePay" status="fsePayStatus"> <tr> <td> <input type="text" name="schemePayoutDTO.schemePayoutListFSE[%{#fsePayStatus.index}].paramValue1"/> <select style="width: 150px;" class="select" name="payoutTypeVal" onchange="showPayoutValue(this)"> <option value="1">Option 1</option> <option value="2">Option 2</option> <option value="3">Option 3</option> <option value="4">Option 4</option> </select> <!-- Pass element instead of value to identify which element has to targeted --> <!-- Keep this outside--> <div class="div1" cssStyle="margin-left:15cm; position: relative; bottom:-45px;"> <!--Change id to class and keep this empty--> </div> </td> </tr> </div> <div value="schemePayoutDTO.schemePayoutListFSE" var="fsePay" status="fsePayStatus"> <tr> <td> <input type="text" name="schemePayoutDTO.schemePayoutListFSE[%{#fsePayStatus.index}].paramValue1"/> <select style="width: 150px;" class="select" name="payoutTypeVal" onchange="showPayoutValue(this)"> <option value="1">Option 1</option> <option value="2">Option 2</option> <option value="3">Option 3</option> <option value="4">Option 4</option> </select> <!-- Pass element instead of value to identify which element has to targeted --> <!-- Keep this outside--> <div class="div1" cssStyle="margin-left:15cm; position: relative; bottom:-45px;"> <!--Change id to class and keep this empty--> </div> </td> </tr> </div> <div value="schemePayoutDTO.schemePayoutListFSE" var="fsePay" status="fsePayStatus"> <tr> <td> <input type="text" name="schemePayoutDTO.schemePayoutListFSE[%{#fsePayStatus.index}].paramValue1"/> <select style="width: 150px;" class="select" name="payoutTypeVal" onchange="showPayoutValue(this)"> <option value="1">Option 1</option> <option value="2">Option 2</option> <option value="3">Option 3</option> <option value="4">Option 4</option> </select> <!-- Pass element instead of value to identify which element has to targeted --> <!-- Keep this outside--> <div class="div1" cssStyle="margin-left:15cm; position: relative; bottom:-45px;"> <!--Change id to class and keep this empty--> </div> </td> </tr> </div>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.