I don't understand why Javascript doesn't work.

Asked 2 years ago, Updated 2 years ago, 135 views

fiscalYear is not defined at Object.<anonymous>

The error appears.I thought the fiscalYear could not be read from this error.I tried console.log("123");` to see if JS is working, but it doesn't print well.I would like to connect the two JS listed below, but I don't understand why they can't be linked.Please let me know m(__)m

$(function(){
    $('input[name="fiscal_year"]').on('change', function(){
        $('select[name="selectyearMonth"] option') .remove();
        $('select[name="selectyearMonth"]').append($("<option></option>"));
        if($(this).val()!=""){ 
            for(i=4;i<=15;i++){
                letm=(i<13)?i:(i-12);
                m = ('0'+m).slice(-2);
                letty=(i<13)?$(this).val():parseInt($(this).val())+1;
                letop=$("<option></option>", {
            value —y+m,
                       html:y+"year"+m+"month"
                });
                $('select[name="selectyearMonth"]').append(op);
            }
        }
    });
});


function setSelect() {console.log("123");
    varselectElement= document.getElementById("setSelect");
    for(vari=1;i<=12;i++){
        var option= document.createElement("option");
        option.value=i;
        option.innerText=i;
        selectElement.appendChild(option);
    }
}
<tr>
    <th>Year/Year>/th>
    <td>
        <input name="fiscal_year" maxlength='4'>year 
        <select name="selectyearMonth" style="width:40%;"id="setSelect">/select>month
        <scriptth:src="@{/js/year.js}"></script>
    </td>

javascript thymeleaf

2022-09-29 22:23

1 Answers

I don't know why Javascript isn't called

I added a more typical way to call js to the description in ↑.Please refer to
Understanding the grammar of Javascript and jQuery seems to be the shortest way to go.

Sample 1, How to write a one-time action when html is ready.

window.onload=()=>{
        // ...
    };

    // If you are using jQuery, there are small differences, but the meaning below is almost the same.
    // *$() is the grammar of jQuery.
    $(function(){
          // ...
      }
    );

Sample 2, how to write when you press button and call the corresponding function.

<input type="button" value="April" onclick="selectSlcMonth(4)">

Typical sample below

<html>

<head>
</head>

<body>
    <form method="POST">
        <select name="slcMonth" style="width:40%;"id="slcMonth">/select>
        <input type="button"value="April" onclick="selectSlcMonth(4)">
        <input type="button"value="May" onclick="selectSlcMonth(5)">
    </form>
    <script>
        // ES2015
        window.onload=()=>{
            initializeSlcMonth();
        };

        function initializeSlcMonth(){
            const selectElement= document.getElementById("slcMonth");
            for(leti=1;i<=12;i++){
                construction= document.createElement("option");
                option.value=i;
                option.innerText=i+"month";
                selectElement.appendChild(option);
            }
        }

        function selectSlcMonth(valMonth){
            const selectElement= document.getElementById("slcMonth");
            selectElement.value=valMonth;
        }
    </script>
</body>

*Another supplementary name attribute, value attribute, class attribute, and id attribute

The name and value attributes are
If you use it for input or select and submit it, it will be sent to the server.

The class attribute is
It is highly recommended to use for css.
You can also use it for some javascripts.

The id attribute is
It is highly recommended that you use it only for javascripts.
(Not recommended for css today)


2022-09-29 22:23

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.