0

I am new to Jquery and I am using Jquery to populate values in a drop down field.

Upon selection of a value from drop down, I am assigning value to a hidden field.

onSelect: function(index,row){
        var val = row.value; 
        alert('val '+val );
                $("#hid").val(val );    
    }

How can I assign the value to JSP variable or if I use request.getParameter("hid"); do I need to submit the form again to get the value?

Edit 1

$(function(){
$('#comb').combogrid({  
    panelWidth:200, 
    url: 'myservice',  
    idField:'id',  
    textField:'desc'  
    columns: [[  
                {field:'Id',title:'Id',width:20},  
                {field:'desc',title:'Desc',width:80}
            ]],
    onSelect: function(index,row){
        var val = row.value; 
        alert('val '+val );
                $("#hid").val(val );    
    }
});

});

Jacob
  • 14,463
  • 65
  • 207
  • 320

2 Answers2

5

JSP is Java code which runs on your server side.

JavaScript runs on your browser.

So you cannot assign JSP variables using JavaScript.

Form submit or ajax is the right choice for this situation.


Ajax Code snippet.

onSelect: function(index, row) {

    $.ajax({
        url: '/your-url', // path to your url which gets this ajax request
        method: 'get', // or a http method you want to use
        data: {
            value: row.value
        },
        success: function(response) {
            alert('Boom!' + response);
        }
    });

}

See jQuery Ajax API docs for more information. There are lots of options.

breadmj
  • 185
  • 11
  • breadmj, I have edited and added my code to populate drop down. Should I wrap with `$.ajax({`? – Jacob Apr 09 '13 at 05:56
  • nope, I edited my answer. You should send an ajax request when your grid is selected. (You use combogrid plugin right?) So locate the ajax code inside the 'onSelect' callback. – breadmj Apr 09 '13 at 06:04
  • breadmj, so `url` inside `$.ajax({` is the url where you would like to submit right? – Jacob Apr 09 '13 at 06:06
  • No, you don't have to do that. It's redundant. As you see, the 'data' option inside the ajax code is the place to locate data you want to send. If you use form submit rather than ajax, then assigning the value to hidden field works. Cheers! – breadmj Apr 09 '13 at 06:22
  • I am able to do the ajax call, however when I try to receive value using `request.getParameter("value ");` it is coming as null, even though I can see in value is being passed in Chrome header. – Jacob Apr 09 '13 at 06:27
  • What framework are you using in server side? SpringMVC? If you use SpringMVC, then you can extract the value by using `@RequestParam` annotation. And before using `@RequestParam`, you should check that how the value is sent. By query string? By request body? Also check that how your code `request.getParameter("value")` tries to extract the parameter. – breadmj Apr 09 '13 at 06:34
  • I am using JSP not `SpringMVC`. Query parameters are sent proeprly, only problem is when I try to receive the value, it is null. – Jacob Apr 09 '13 at 06:38
  • Hmm.. I can't understand how it could be null. Try `request.getQueryString()` to check the value was sent correctly. If you're stuck in this problem, then I think asking another question and providing additional information will be better. – breadmj Apr 09 '13 at 06:54
1

Based on user's selection of the dropdown, if you want to do someting on the server side, I would suggest to send AJAX call or Form submit.

You wont be able to do the below directly.

request.getParameter("hid"); 
Jaydeep Rajput
  • 3,605
  • 17
  • 35