To receive a value of type="hidden" in JavaScript

Asked 2 years ago, Updated 2 years ago, 81 views

In the time leaf, pass the variables from the two lists, the stationNameList and the stationIdList, to JavaScript.


in two lists of variables received <input type="text"th:field="*{ationName}"onclick="check()"id="ationName">
The ID that matches the name entered in the input tag above is

<input type="hidden" th:field="*{ationId}">
I would like to display it with the input tag above.

At that time, I would like to ask about the conditional expression in JavaScript.
For example, I would like to obtain the ID of the teacher in charge as the value entered by Taro Yamada.
For Taro Yamada's stationName, I would like to know the JavaScript to obtain the teacher's stationId.

function check(param){
    variationName;
    variationId;
    if(ationId!=null){
        document.getElementById("ationId");
    } else {
        document.getElementById("ationId").value;
    }
}
<input type="text" th:field="*{ationName}"onclick="check()"id="ationName">
<input type="hidden" th:field="*{ationId}">

HTML

<scriptth:inline="javascript">
    /*<![CDATA[*/
        varobjName=/*[[${ationNameList}]]*/{};
        varobjId=/*[[${ationIdList}]]*/{};
    /*]]>*/
</script>

javascript thymeleaf

2022-09-29 22:55

1 Answers

Originally, input with type="hidden" should define the name attribute appropriately, but I think you can get it by using nextElementSibling.

https://developer.mozilla.org/en-US/docs/Web/API/NonDocumentTypeChildNode/nextElementSibling

Alternatively, depending on what th:field actually prints, you can define attributes such as data-text-id and refer to them in querySelector.

https://developer.mozilla.org/ja/docs/Web/API/Document/querySelector


2022-09-29 22:55

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.