how to use grails ${createLink} in javascript

二次信任 提交于 2020-01-25 05:26:55

问题


i have a javascript function as follows

function GetSelectedItem()
{
    var e = document.getElementById("country");
    var strSel =  e.options[e.selectedIndex].value;
    alert(strSel);
    var url = "${createLink(controller:'country', action: 'wholeTestUnits', id: strSel)}"
    alert(url);
 }

i want to go to that url action when i click the submit button like

<button class="submit_small" onClick="GetSelectedItem();">
    <span><g:message code="default.button.submit.label" /></span>
</button>

This ${createLink} is not working.


回答1:


A better way of doing this which doesn't require the JavaScript code be in your GSP would be the following:

<button class="submit_small" onClick="GetSelectedItem();" data-url="${createLink(controller:'country', action: 'wholeTestUnits')}">
    <span><g:message code="default.button.submit.label" /></span>
</button>

function GetSelectedItem() {
    var button = event.target;
    var e = document.getElementById("country");
    var strSel =  e.options[e.selectedIndex].value;
    var url = button.getAttribute("data-url") + "/" + strSel;
}



回答2:


I think you have a serverside/clientside problem. The createLink is run on the server, the JS is run on the client...

Try:

var url = '${createLink(controller:'country', action: 'wholeTestUnits')}' + strSel ;



回答3:


As I think , you are not getting value of strSel in your link. You can try this.

function GetSelectedItem()
{
        var e = document.getElementById("country");
        var strSel =  e.options[e.selectedIndex].value;
        alert(strSel);
        var url = "${grailsApplication.config.grails.serverURL}/country/wholeTestUnits/" + strSel
        alert(url);
}



回答4:


Instead of using the createLink you could build your own URL and use that one instead. You have to pay attention to capital letters in the controller name, though.

var url="${ createLink(controller:'testcontroller', action:'getData') }";

is equivalent to

var url = "/testcontroller/getData;

If you want to pass in arguments from javascript to the controller you can do like this.

var url = "/testcontroller/getData?arg0=" + arg0 + "&arg1=" + arg1;

To extract the arguments in the controller you do use the params keyword. So to print the parameters in the controller you do this:

println params.arg0
println params.arg1



回答5:


    --in gsp file--
   <a href="#" onclick="callAjax('${createLink(controller:'shift',action: 'addShift')}');" >Add/Edit Shift</a>


    --in js file--
    function callAjax(path){
    //path->/shift/addShift
       var xmlhttp = new XMLHttpRequest();
                xmlhttp.onreadystatechange = function() {
                    if (this.readyState == 4 && this.status == 200) {
                        document.getElementById("updateContent").innerHTML = this.responseText;
                    }
                };
                xmlhttp.open("GET", path, true);
                xmlhttp.send();
      }



回答6:


try this: var url = "${createLink(controller:'country', action: 'wholeTestUnits', params:[id: strSel], absolute: true)}"



来源:https://stackoverflow.com/questions/18760665/how-to-use-grails-createlink-in-javascript

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!