I'm quite a newbie when it comes to jquery. I'm using cakePHP and I have a form to register orders for a selected user, the select input for selecting the user looks like this in cakePHP:
echo $this->Form->input('user_id');
and that renders a form with an input like this:
<label for="OrderUserId">User</label>
<select name="data[Order][user_id]" id="OrderUserId">
<option value="2"> User 2</option>
<option value="3"> User 3</option>
</select>
I want to do an ajax call so that, when a user is selected, his info appears in a div. So far, I have this javascript:
$(document).ready(function(){
$("#OrderUserId").change(function() {
$.ajax({
url: 'users/getData' //here would go the user ID
});
});
});
My question is this: how can I get the value of the selected option with jQuery so that I can pass it to the url in the ajax method?
Just use plain js this.value
. this
inside your callback represents the dom element so you would just access its value using .value
.
$(document).ready(function(){
$("#OrderUserId").change(function() {
//var user = this.value;
$.ajax({
url: 'users/getData/' + this.value //here would go the user ID
});
});
});
$(document).ready(function(){
$("#OrderUserId").change(function() {
var _this=$(this);
$.ajax({
url: 'users/getData'+_this.val(); //here would go the user ID
});
});
});
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.