javascript - 在 thymeleaf 中调用 javascript 函数

标签 javascript html onclick thymeleaf

我需要在 thymeleaf 中调用 javascript 函数,但由于未定义函数而收到错误。 这是我的代码

<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();'" 你可以引用这个链接 Javascript function call with Thymeleaf

关于javascript - 在 thymeleaf 中调用 javascript 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41069031/

相关文章:

javascript - 构造一个日期对象和时区

javascript - 从 URL 捕获顶级域名时出现问题

javascript - 将以编程方式生成的音频流传输到浏览器

javascript - 如何使用 hello.js 获取用户电子邮件 ID?

c# - onclick后如何调用onclientclick

JavaScript:相对于父级单击时增加变量值

javascript - Meteor 基础应用 : ReferenceError: Entries is not defined

javascript - 在 DOM 之后加载新元素后检测点击

html - <PICTURE> 的后备图片

android - 我怎样才能使用 jfeinstein slider 库进行任何其他 Activity