作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我在JavaScript中有一个函数,可以将消息提交到Grails Controller 中的方法,同时使用myID id更新div。
function messageKeyPress(field,event,messageBox) {
...
var message = $('#messageBox').val();
<g:remoteFunction action="submitMessage" params="\'message=\'+message" update="myID"/>
...
}
<div id="chatMessages" class="chatMessages"></div>
<input type="text" id="messageBox" class="messageBox" name="message" onkeypress="messageKeyPress(this,event,'#messageBox');"/>
<div id="myID">
onkeypress="messageKeyPress(this,event,'#messageBox', '#myID');"
function messageKeyPress(field,event,messageBox, myID) {
...
<g:remoteFunction action="submitMessage" params="\'message=\'+message" update="${myID}"/>
最佳答案
我建议您改用jQuery。默认情况下,它 bundle 在Grails项目中。结果,您将在javascript代码和gsp View 逻辑之间得到清晰的区分。例如,application.js可能如下所示:
(function($) {
$('.messageBox').on('keypress', function () {
...
var params = {message: $(this).val()};
var url = $(this).data('url');
var target = $(this).data('target');
$.post(url, params, function(response) {
$(target).html(response);
});
...
});
})(jQuery);
<input type="text" id="messageBox"
class="messageBox" name="message"
data-url="${createLink(action: 'submitMessage')}"
data-target="#myId"/>
<div id="myID"></div>
messageBox
css类。在每个字段的
data-target
属性中,您可以为所有应更新的div指定一个选择器。
关于javascript - 如何将JavaScript变量传递给g:remoteFunction的 “update”属性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21076183/
我是一名优秀的程序员,十分优秀!