How can I set an onchange event to a tag?
I want to transfer the new value to the server without klicking the submit button (therefore in an ajax way)
Grails datepicker tag implemention renders a fixed number of select tags based on the precision that you choose. However, no onchange attribute is added to any of these select tags. The id of each select tag is the id that you specify in the datepicker tag appended by the type of data that each select shows. Eg. the select box for day has id "yourDatepickerTagId_day", the month select tag has id "yourDatepickerTagId_month" etc. To bind the "change" event to any of these select tags you will have to use some custom javascript to handle these events. If you use jQuery, you can use something like this:
$(document).ready(function(){
$("select[id ^= yourDatepickerTagId]").change(function (){
// Do your stuff here...
});
});
Related
I created a Cascading of Dropdown list for Country state city in my application using Ajax request and Select Box. My application gathers information about users ie personal info etc. In my application there is an option to Edit the entered fields.. upon clicking the edit button the personal information page is populated with previously entered data. Now i need help to show the selected value that is which country, state and city is selected by the user in my select box dropdown. Please help!!
In your view assign the previously selected value to a javascript var. Then when the ajax request succeeds, set the dropdown value from the var.
Here's an example using jQuery.
// set a js var from a php var passed to the view from CI
var previous_country = <?=$previous_country?>;
$.ajax({
// your ajax options here (url, datatype, etc)
success: function(response){
// code to populate dropdown here
// then set the dropdown value to change the selected item:
$('select#country').val(previous_country);
}
});
I have a select box in my view.ctp , it shows some content from the controller, but my problem is i want to load content dynamically according to the droapdown selection
i tried with making ajax request on onchange of selection in the select box. but the problem is ,the select box id is sending to the controller function, their i set the content for display .but the page is not refreshing..
I dont know which logic sholud use for this purpose..if anybody have an exerience with this..please replay me..
In order to achieve those results you should follow the next steps:
Create an action that returns the content with $this->layout = null
Create a view for this action that displays the content in the way you desire (html with no css)
In the view that you like to fetch that content through AJAX make a request that pulls that controller and view
append it where you want it on your page (with proper css on the page you make the request it will display correctly)
I have a form in which the user can save. In this form I have a dynamic SELECT, when user chooses an option with the event ONCHANGE I get the result of another SELECT. The problem is that when user loads his saved form I need to select with JavaScript his selected option(saved before) but I can't to get the onchange event to load the other select.
This is using jquery. On the dom ready, you select the value for your first select box, then run the function to populate the second select. Next, you're selecting the right value for that second box. On the change, you re run the function to populate your second select
$(document).ready(function(){
$("#firstselect").val(the value you wanted for the first select);
function that populates values of your second select box();
$("#id of your second select").val(the value you wanted for the second select);
$('firstselect').change(run your function that populates your second select)
});
I know MVC doesn't have autoposback functionality and that needs to be done using JS/JQuery, and that is when my problems start... don't know how to do it yet.
This is how I populate my ddl:
#Html.DropDownListFor(x => x.CurrentCountry,
new SelectList(Model.Countries.ToList(), "Code", "Name"))
and my URL have this format:
localhost/Products/?country=US¤cy=USD&category=17&page=2
How do I add postback functionality to take the selected country?
Thanks.
I know MVC doesn't have autoposback functionality and that needs to be done using JS/JQuery
Not necessary. You could put the dropdown in an HTML <form> and when the user submits this form the selected value will be sent to the server.
If on the other hand you want to transmit the selected value when the user changes selection then you need to use javascript and subscribe to the onchange event. For example if you use jQuery:
$(function() {
// subscribe to the change event of the dropdown
$('#CurrentCountry').change(function() {
// find the containing form and submit it
$(this).closest('form').submit();
});
});
Hello sir I am new to the jsp and ajax world. my problem is
If i select one combo option then it should change the other combo options dynamically without submit button press.
for example if i select the country then it should shows their states in other combo.
I am using servlet & JSP and MS-ACCESS as backend. please reply as soon as possible.
THANKING YOU....
You need:
A piece of JSP which returns a string which can be parsed in javascript to create a list of possible combobox items (value, name) to select in the SECOND combobox, based on a VALUE which can be selected in the FIRST combobox
A piece of javascript which receives the onChange event of the FIRST combobox and uses the then selected value to create a XMLHTTP-request to your JSP (at 1)
A piece of javascript which you can pass to the XMLHTTP-request onReadyStateChange event to execute, which reads/parses the parseable string (from 1) and fills the second COMBOBOX with those values
Good luck