Using jQuery .change() to change values in a <p> element
I have a select element with a few options for currencies, and then a <p>
element at the bottom that currently p开发者_如何学Crints the current exchange rate for USD to another currency. What I'm trying to do, is upon .change()
of the select element value, I want to insert the value of the select element into a PHP script, and then re-.load()
the <p>
element.
How can I pass a value from jQuery .val() to a PHP script?
You might want something like:
$('select').change(function() {
$('p').load('/path/to/script.php?myVar=' + $(this).val());
});
Of course, you might want to use more specific selectors.
Use Ajax. Here's an example of how to do this with JQuery:
$('#selectElement').change(function(){
var currentSelection = $('#selectElement').attr('value');
$.get('yourScript.php?selection=' + currentSelection, function(data){
$('#pElement').html(data);
})
});
Or something like that. Not tested.
jQuery('#mySelect').change(function(){
jQuery.ajax({
url:'myPhpFile.php'
type:'get'
data:{currencyId:jQuery(this).val()},
success:function(data)
{
jQuery('#myDiv').html(data);
}
});
});
精彩评论