Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/78.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何在JSP中动态更改迭代逻辑中文本元素的id?_Javascript_Jquery_Html_Jsp_Struts - Fatal编程技术网

Javascript 如何在JSP中动态更改迭代逻辑中文本元素的id?

Javascript 如何在JSP中动态更改迭代逻辑中文本元素的id?,javascript,jquery,html,jsp,struts,Javascript,Jquery,Html,Jsp,Struts,以下是我使用的代码: <% int number=0 %> <logic:iterate name="sample" id="sample1" property="lstWaferRequests" indexId="rowid"> <tr> <% number= (rowid.intValue())+1;%> <td> <html-el:text name="sample1" property="st

以下是我使用的代码:

<% int  number=0 %>  
<logic:iterate name="sample" id="sample1" property="lstWaferRequests" indexId="rowid"> 
<tr>
     <% number= (rowid.intValue())+1;%> 
<td>
    <html-el:text name="sample1" property="strExpDate" styleId='<%="Date"+number%>'/>   
</td>

<script>
    var x=document.getElementById("<%="Date"+number%>");
</script>

<a href="javascript:show_calendar(x,'');">
    <img src="images/calendar.gif" width="16" height="16" border="0" alt="Click Here to Pick up the timestamp"></a>
</td>
</tr>
</logic:iterate> 

var x=document.getElementById(“”);
我试图增加一个变量(数字),并将其添加到
sample1 textbox
的id中,但在下一次迭代中,id没有改变。我使用IE11中的
检查元素选项检查了它

我在javascript中使用了
setAttAttribute()
方法来设置ID,但我得到了一个错误

对象不支持setattribute方法

我需要动态更改存储日期值(sample1)的文本框的ID,因为我需要将其作为参数传递给
show\u calendar
方法


请告知。

我已将类添加到您的
文本框和
anchortag

通过您的链接,我知道您必须传递textbox not
id
name
属性

因此,我添加了动态名称。这可以通过
strExpDate
实现。但是我建议使用
JSTL
而不是scriptlet。 有关详细信息,请参见
javaexpert
post

更新: 在你的逻辑迭代中,你必须这样改变

<% int number=1 %>  
<logic:iterate name="sample" id="sample1" property="lstWaferRequests" > 
    <tr>
        <td>
            <html-el:text name="strExpDate<%=number%>" property="strExpDate" styleClass="dates" />   
        </td>
        <td>
            <a class="test" href="#">
                <img src="images/calendar.gif" width="16" height="16" border="0" alt="Click Here to Pick up the timestamp">
            </a>
        </td>
    </tr>
     <% number++; %>
</logic:iterate> 

看。如果有任何澄清,请告诉我,希望这有帮助。

将数字变量转换为字符串并连接到indexId。
然后再次返回int。它应该可以使用$('your element').attr(“id”,“assign id”);你也可以发布你的
show_calendar
函数吗?谢谢:)我会尝试一下,但是有没有办法改变动态传递给JavaScript方法的参数?我在这个URL中找到了日历脚本?所以你想在
show_calendar
中传递
元素名称和
?谢谢对于此:),但我没有预定义要添加的文本框的数量。我通过单击add row按钮调用JavaScript方法来动态添加它:(您不必在任何地方预定义文本框的数量。为了刺激您的问题,我使用了5行文本框。此函数可以处理任意数量的行。希望我在回答中提到它。谢谢:)你能告诉我href=“#”在这里有什么用吗?它将如何触发所需的jquery代码?有关
href=“#”
详细信息,请参阅。但是这里
href
什么都不做。如果没有这一点,它将起作用。看见我已经动态地创建了所有的行。考虑做一个或者,它可以对其他人有用。
$(document).ready(function() {
    $(".test").click(function(e) {
        var value = $(e.target).closest('tr').find('.dates').val();
        var name = $(e.target).closest('tr').find('.dates').attr('name');
        alert("Name : "+name+" Value : "+value);

        // You can call show_calendar() from here by passing name and value.
    });
});