[英]jquery - get the value of a selected option in a select input
關於jQuery,我是一個新手。 我正在使用cakePHP,並且有一個表單可以為所選用戶注冊訂單,用於選擇用戶的選擇輸入在cakePHP中如下所示:
echo $this->Form->input('user_id');
並使用以下輸入呈現一個表單:
<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>
我想進行ajax調用,以便在選擇用戶時,他的信息出現在div中。 到目前為止,我有這個javascript:
$(document).ready(function(){
$("#OrderUserId").change(function() {
$.ajax({
url: 'users/getData' //here would go the user ID
});
});
});
我的問題是:如何使用jQuery獲取所選選項的值,以便可以將其傳遞給ajax方法中的url?
只需使用普通js this.value
。 回調中的this
代表dom元素,因此您可以使用.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
});
});
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.