Grails TagLib获取元素值

Grails TagLib Get element Value

本文关键字:元素 获取 TagLib Grails      更新时间:2023-09-26

我有一个自己的TagLib和一个名为boxLink的标签来生成远程链接并打开Modal:

Closure boxLink = { attrs, body ->
    Integer modalId = OwnUtil.getRandomNumber(1000)
    Map params = attrs.params ? attrs.params : [:]
    params.put('modalId', modalId)
    Map linkParams =  params

    out << '<a href="#" class="modalBoxLink ' + attrs.class
    if (attrs.title)
        out << ' tooltipSpan" title="' + attrs.title
    out << '" onclick="'
    out << remoteFunction(controller: attrs.controller, action: attrs.action, onLoading: attrs.onClick + ';loadingSpinner()', onComplete: 'removeSpinner()',
            onSuccess: 'viewModalBox(data,' + modalId + ');initForm();', onFailure: 'errorAlert();', params: linkParams)
    out << '">'
    out << body()
    out << '</a>'
}

在某些情况下,我需要读取Form Element的值以将其放入AJAX请求的数据中,因此我尝试了

if(attrs.elementid){
    def elParam = attrs.elementid
    if(attrs.elementname)
        elParam = attrs.elementname     
    linkParams.put(elParam,"document.getElementById('${attrs.elementid}').value")
 }

生成ajax请求的数据值

data:{'modalId': '357','application': 'document.getElementById('elementid').value'}

如何通过获取HTML元素的值来设置ajax请求的数据属性?

标记库在服务器上执行,您希望生成的代码作用于客户机。这只能通过javascript实现。您最好使用jQuery和ajaxForm。您可以将函数绑定到模态打开或其他操作,并设置表单中隐藏字段的值。

下面是我使用的一个例子,它从一个隐藏的div中填充html的模态,并向两个隐藏的字段添加数据。请注意,我没有使用ajaxForm在这里,但它是相同的想法。

<script type="text/javascript">
function configModal() {
    $(".clickable-row").click(function() {
        var row = $(this)
        var modal = $('#notificationModal');
        var title = row.find('td').find('a').html();
        var html = row.find('.notification-content').html();
        var notificationId = row.attr('id');
        modal.find('.modal-body').html( html );
        modal.find('.modal-title').html( title );
        modal.find('#modal-form').find('input[name=id]').attr('value', notificationId)
        modal.find('#modal-form').find('input[name=isDeleted]').attr('checked', row.hasClass('deleted'));
        modal.modal();
        $.post("${createLink(controller: 'notification', action: 'update')}",
                { id : notificationId, isRead : true },
                function() { row.addClass('read'); });
    });
}
$(document).ready(configModal);
</script>