我加載了一個表單並通過AJAX從PHP文件動態填充選擇。在實現動態AJAX填充選擇之前,我的更改功能起作用(當用戶選擇「其他」時,它只顯示另一個輸入)。現在更改功能不起作用。爲什麼在用AJAX加載html之後沒有jQuery更改函數?
我知道ready函數正在觸發,因爲jStepper函數運行。我已經嘗試過在ready函數內外使用change函數。我有一種感覺,在AJAX獲取完成之前加載了更改函數,但這確實很重要嗎?
var types = "<select name='ve_categoryNo' id='ve_categoryNo'>";
var d = new Date();
$.get('scripts/vehicle_category_feed.php?date=' + d.getTime(), function ($type)
{
$($type).find('type').each(function()
{
types += "<option value='" + $(this).attr("categoryno") + "'>" + $(this).attr("category") + "</option>";
});
types += "<option value='other'>Other(Specify)</option></select>";
$('#ve_categoryNo_td').html(types);
});
$(document).ready(function()
{
$('input[type=text]').click(function()
{
$(this).select();
});
$('#vehicle_entry').ajaxForm(function()
{
showMessage('vehicle_information_added');
});
$('#ve_ariNo').jStepper({minValue: 1, maxValue: 99999999});
$('#ve_fleetNo').jStepper({minValue: 1, maxValue: 999999999});
$('#ve_vehicleYear').jStepper();
$('#ve_purchasePrice').jStepper({minValue: 0});
$('#ve_categoryNo').change(function()
{
if ((this.value) == "other")
{
$('#otherCategory').show();
$('#otherCategory input[type=text]').focus();
} else
{
$('#otherCategory').hide();
}
});
});
[動態創建的元素上的事件綁定?]的可能重複(http://stackoverflow.com/questions/203198/event-binding-on-dynamically-created-elements) – showdev 2016-07-05 22:27:58