ユーザーがドロップダウン リストから要素を選択したときに、AJAX 呼び出しを実行しようとしています。イベントが発生するとすぐに.mouseup
、AJAX リクエストを起動してデータを送信する必要があります。
これが私が持っているものです:
$('<select />')
.attr('name', 'status')
.append('<option>Open</option>', '<option>Full</option>', '<option>Canceled</option>')
.appendTo(this);
$('select[name=status]').mouseup(function () {
$.ajax({
url: '/ajax/training-update.php',
data: {status: $currentSelection},
type: 'POST',
success: function(output) {
alert(output);
}
});
});
これにより、ドロップダウンから項目を選択すると無限ループが発生します。私は何を間違えましたか?
編集:
@Kolink が以下で提案したように、に変更.mouseup
しました.change
。これにより、無限ループと「Illegal Invocation」エラーが発生しました。
以下のテストコードを試して、.change
正しく実装したことを確認しました。
$('select[name=status]').change(function() {
alert('Handler for .change() called.');
});
これは期待どおりに機能します。
で AJAX を使用できない理由はあります.change
か?
編集#2:完全なスクリプトが追加されました
<script>
$(function() {
var $rowStartDate = $("span[id^=rowStartDate]");
var $rowEndDate = $("span[id^=rowEndDate]");
var $location = $(".location");
var $status = $('.status');
var $ajaxSubmit = $('#ajaxSubmit');
$($rowStartDate.add($rowEndDate)).click(function() {
var $currentSelection = $(this);
// if there is already an input-field in this TD, return...
if($currentSelection.find('input').length > 0)
return;
$currentSelection.html('');
$currentSelectionClass = $currentSelection.attr('class');
//create new input-field-object
if($currentSelectionClass == "rowStartDate"){
$('<input type="text" />')
.attr('name', 'editStartDate')
.addClass('editStartDate')
.appendTo(this)
.datepicker();
}
if($currentSelectionClass == "rowEndDate"){
$('<input type="text" />')
.attr('name', 'editEndDate')
.addClass('editEndDate')
.appendTo(this)
.datepicker();
}
});
$($location).click(function () {
var $currentSelection = $(this);
// if there is already an input-field in this TD, return...
if($currentSelection.find('select').length > 0)
return;
$currentSelection.html('');
$('<select />')
.attr('name', 'location')
.append('<option>Location 1</option>', '<option>Location 2</option>', '<option>Location 3</option>')
.appendTo(this);
});
$($status).click(function () {
var $currentSelection = $(this);
// if there is already an input-field in this TD, return...
if($currentSelection.find('select').length > 0)
return;
$currentSelection.html('');
$('<select />')
.attr('name', 'status')
.append('<option>Open</option>', '<option>Full</option>', '<option>Canceled</option>')
.appendTo(this);
});
//AJAX call not working correctly
$('select[name="status"]').change(function () {
$.ajax({
url: '/ajax/training-update.php',
data: {status: $currentSelection},
type: 'POST',
success: function(output) {
alert(output);
}
});
});
//Original AJAX implementation. Moved to above.
// $($ajaxSubmit).click(function () {
// $.ajax({
// url: '/ajax/training-update.php',
// //data: {action: 'test'},
// type: 'POST',
// success: function(output) {
// alert(output);
// }
// });
// });
// $("#ajaxError").ajaxError(function(event, request, settings){
// $(this).append("Error requesting page " + settings.url);
// });
});
</script>