我有以下的HTML代碼:如何在發送AJAX請求之前捕獲#select1和#select2值?
<select id="select1">
<option value="0">Option 0</option>
<option value="1">Option 1</option>
<option value="2">Option 2</option>
<option value="3">Option 3</option>
</select>
<select id="select2">
<option value="0">Option 0</option>
<option value="1">Option 1</option>
<option value="2">Option 2</option>
<option value="3">Option 3</option>
</select>
<select id="select3">
<option value="0">Option 0</option>
<option value="1">Option 1</option>
<option value="2">Option 2</option>
<option value="3">Option 3</option>
</select>
<input type="text" id="select4">
我把它們變爲選擇二元素,我改變輸入是一個SELECT和事後選擇二如下:
$(function() {
$('#select1').select2();
$('#select2').select2();
var field_value = $('#select2 :selected').val();
var module = $('#select1 :selected').val();
$('#select3').select2().on('select2:select', function(ev) {
var condition_type = $(this).val();
var select_el = '<select id="select4" style="width: 75%" multiple="multiple"></select>';
$('#select4').replaceWith(select_el);
$('#select4').select2({
placeholder: 'Start typing ...',
tags: true,
ajax: {
delay: 200,
url: '/someUrl',
minimumInputLength: 2,
data: function(params) {
var query = {
keyword: params.term,
condition: condition_type,
field: field_value,
table_name: module
}
return query;
},
processResults: function (data) {
return {
results: data
};
}
}
});
});
});
這是目前工作沒有任何問題。只要我發送了AJAX調用,#select1, #select2
的值就會作爲REQUEST參數發送,但如果我更改#select1, #select2
的值並嘗試相同的AJAX請求,則會發送先前的值而不是新的值。
我曾嘗試以下,但不工作:
$('select#conditions').select2().on('select2:select', function (ev) {
var condition_type = $(this).val();
...
}).on('select2:selecting', function (ev) {
field_value = $('#select2 :selected').val();
module = $('#select1 :selected').val();
});
所以我的問題是怎麼做的我抓住新的價值?任何幫助?我試圖設置一個小提琴示例here,但我無法使其與AJAX調用一起工作。
看起來你」已緩存val ('#select2:selected')。val(), table_name:$('#select1:selected')。val()'在'field_value'和'module'中只需要使用''' ''在瀏覽器中緩存的ajax – br3t
@ br3t是什麼意思?如果是的話無論如何要禁用此默認行爲? – ReynierPM
緩存在變量 – br3t