从自定义html属性获取值

时间:2022-11-26 23:06:47

I took the maintenance of a really old java web application. (Recently it was passed from tomcat 4 to tomcat 6, and maybe this caused the problem.)

我修了一个非常古老的java Web应用程序。 (最近它从tomcat 4传递到tomcat 6,也许这导致了问题。)

In the jsp there is a table that have inside this column (where elenco is a java array of objects and elenco[y] means a different object for each row of the table). So basically it is a radio button for every row:

在jsp中有一个表在此列中(其中elenco是一个java对象数组,elenco [y]表示表的每一行的不同对象)。所以基本上它是每一行的单选按钮:

<form name="index" action="elencoRichiesteFotoImpronte.jsp">    
            <td width="5%"><div align="center"><input type="radio" value="<%=elenco[y].getId()%>" value1="<%=state%>" value2="<%=elenco[y].getNameOp()%>" data-val2="<%=elenco[y].getNameOp()%>" name="selrec" id="selrec" ></div></td>
</form>

(I added the attributes data-val2, to make some attempts with html-5, but didn't worked. And I added too the id, because in the code that I inherit was not present). In the same jsp there're a lot of parts in javascript that access to the selected value of radiobutton, for example:

(我添加了属性data-val2,用html-5进行了一些尝试,但是没有用。而且我添加了id,因为在我继承的代码中不存在)。在同一个jsp中,javascript中有很多部分可以访问所选的radiobutton值,例如:

      alert(document.index.selrec.value2);

But it shows that the value is undefined and so most of the code doesn't work. Does exist an easy way to access to the selected custom value of the radiobutton without making big changes to all the code (maybe some HTML tags)? (And possibly without using jquery.)

但它表明该值未定义,因此大多数代码都不起作用。是否存在一种简单的方法来访问所选的自定义按钮的自定义值,而无需对所有代码(可能是某些HTML标记)进行大的更改? (可能没有使用jquery。)

1 个解决方案

#1


1  

Yes this can be done.

是的,这可以做到。

alert(document.getElementById('selrec').getAttribute("value2"));

This accesses the custom attributes of the Radiobutton

这将访问Radiobutton的自定义属性

#1


1  

Yes this can be done.

是的,这可以做到。

alert(document.getElementById('selrec').getAttribute("value2"));

This accesses the custom attributes of the Radiobutton

这将访问Radiobutton的自定义属性