繁体   English   中英

在百里香中调用javascript函数

[英]Call javascript function in thymeleaf

我需要调用javascript函数inn thymeleaf,但由于未定义函数,因此出现错误。 这是我的代码

<div class="input-field col s6 m6">
        <input id="isbn" name="isbn" type="text" class="validate" />
        <label for = "isbn">Enter ISBN Code</label>
</div>
    <div class="input-field col s6 m6">
        <button id="submitCode" class="btn waves-effect waves-light col m4" th:onclick="'javascript:myFunction();'" value="data">ISBN Data</button>
    </div>

JavaScript代码

function myFunction()
{
    var isbn = document.getElementById('isbn').value;
    alert(isbn);
    var xmlhttp = new XMLHttpRequest();
    var url = "https://www.googleapis.com/books/v1/volumes?q=isbn:" + isbn;
    xmlhttp.onreadystatechange = function()
    {
        if (xmlhttp.readyState == 4 &amp;&amp; xmlhttp.status == 200)
        {
            var x = JSON.parse(xmlhttp.responseText);
            callback(x);
        }
    };
    xmlhttp.open("GET", url, true);
    xmlhttp.send();
}
function callback(x)
{
    //do things with your data here
    alert(JSON.stringify(x));
    console.log(x);
}

也许您应该尝试th:onclick="'myFunction();'"您可以使用Thymeleaf引用此链接Javascript函数调用

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM