简体   繁体   English

如何根据我的 Java 代码中的条件有条件地调用 JavaScript 中的警报()?

[英]How do I conditionally call an alert() in JavaScript based on conditions from my Java code?

I am trying to display an alert (a "Congratulations;" message) if data is recorded (through a form on the web page) that meets or exceeds a set Goal (that has already been recorded and stored), I was also considering maybe conditionally playing a cute little sound to go with it.如果记录的数据(通过 web 页面上的表格)达到或超过设定的目标(已经记录和存储),我正在尝试显示警报(“恭喜”消息),我也在考虑也许有条件地用它给 go 播放一个可爱的小声音。 but I would like to get the pop-up working first of course, I am pretty novice with JavaScript.但我当然想先让弹出窗口工作,我对 JavaScript 很陌生。 so I hope this issue isn't a stupid oversight on my part.所以我希望这个问题不是我的愚蠢疏忽。 I did do some digging online and couldn't quite find a solution to fit exactly what I am trying to do here.我确实在网上进行了一些挖掘,但找不到完全适合我在这里尝试做的解决方案。

So, in my Java code I check if the data being recorded meets or exceeds the goal, and if it does then a boolean variable in the Goal object is set to true (to reflect that the Goal was completed).因此,在我的 Java 代码中,我检查正在记录的数据是否满足或超过目标,如果满足,则目标 object 中的 boolean 变量反映了目标已完成。 That boolean variable is being updated correctly. boolean 变量正在正确更新。 However, in my web page I cannot get the alert to pop up when wrapped in the conditions I have it in.但是,在我的 web 页面中,当包裹在我拥有的条件下时,我无法弹出警报。

I know for a fact that the line of Java code below is being seen because I put a print statement before and after it, and both are printing.我知道下面的 Java 代码行正在被看到,因为我在它之前和之后放置了一个打印语句,并且两者都在打印。

Here is the related Java code:这里是相关的Java代码:

model.addAttribute("goalMet", true);

And here is the related HTML and JS (I am using Thymeleaf):这是相关的 HTML 和 JS(我使用的是 Thymeleaf):

<div th:if="${goalMet}">
<p> abc123 </p>
<script th:inline="javascript">
    function congratulate() {
      alert("Congratulations on achieving your Goal!");
    }
    congratulate()
</script>
</div>

The abc123 was just thrown in there for debugging, and it currently does not show up after recording data that meets a goal. abc123 刚刚被扔进去调试,目前在记录符合目标的数据后没有出现。 I have tried a few variations of the script tag, both with and without the th:inline="javascript" part.我已经尝试了脚本标签的一些变体,无论有没有 th:inline="javascript" 部分。 I also tried moving the function definition into the html head tags and just calling congratulate() in this conditional block.我还尝试将 function 定义移动到 html 头标签中,并在此条件块中调用 congratulate() 。 All of my attempts so far have been unsuccessful.到目前为止,我所有的尝试都没有成功。

The only effects I am seeing after recording data that meets a goal is that the URL gets "?goalMet=true" appended to it.在记录满足目标的数据后,我看到的唯一效果是 URL 附加了“?goalMet=true”。

Thanks to Randy Casburn's suggestions, I was able to get this working using Redirect Attributes.感谢 Randy Casburn 的建议,我能够使用重定向属性来完成这项工作。 Here is what I did:这是我所做的:

Changed改变了

model.addAttribute("goalMet", true);

To

redirectAttrs.addFlashAttribute("goalMet", true);

And I added我补充说

"RedirectAttributes redirectAttrs" as a parameter to the method that does the redirect. “RedirectAttributes redirectAttrs”作为进行重定向的方法的参数。

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

相关问题 如何从Wicket中的JavaScript代码调用Java代码? - How do I call Java code from JavaScript code in Wicket? 如何修改我的JavaScript代码以提醒这一点? - How do I modify my javascript code to alert this? 我如何有条件地显示一个Javascript alert()来阻止页面提交,直到用户单击“确定”为止? - How do I conditionally display a Javascript alert() that blocks the page from submitting until the user clicks “OK”? 如何根据 prop 有条件地渲染组件的颜色? - How do I conditionally render the color of my component based on the prop? 如何从 asp.net 中的代码调用 Javascript 警报 - How to call Javascript alert from code behind in asp.net 如何让这个JavaScript程序根据提示输入提醒对象属性? - How do I get this JavaScript program to alert an object property based on the input from a prompt? 如何立即从VB代码中调用javascript函数 - How do I call a javascript function immediately from the vb code 如何从 JavaScript 调用 Java 实例的方法? - How do I call a method of a Java instance from JavaScript? 如何在JavaScript中为图片添加工具提示(基于条件)? - How do I add a tooltip (based on conditions) to an image in javascript? 怎么警告让javascript中断? 我可以用代码吗? - how alert make javascript interrupt? can I do it with code?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM