我有以下代码,如果用户从下拉列表中选择“其他”,它应该向表单添加一个字段。但是,使用此代码似乎会覆盖下拉列表的值,以便在发布表单时$_POST['enquiry_source']
为空。
我已将其缩小到这一行,这导致我在下拉列表中的任何更改上添加该字段时出现问题,而不仅仅是选择了“其他” -
$(field_to_append).insertAfter('#form-field-enquiry_source');
我也试过$('#form-field-enquiry_source').after(field_to_append);
,但结果是一样的。
$(function(){
/** Looks for changes to the enquiry source dropdown */
$('#form-field-select-enquiry_source').live('change', function(){
/** Check the enquiry source */
var enquiry_source = $('select[name="enquiry_source"]').val();
/**
* Adds another field to the enquires form when the user selects 'Other' form the enquiry source dropdown
*/
if(enquiry_source === 'other'){ // The user has selected other as the enquiry source, so lock the form
var field_to_append = '<div id="form-field-enquiry_source_other" class="form-field float-left">'+
'<label>Other<span class="required"></span></label>'+
'<input name="enquiry_source" id="form-field-input-enquiry_source_other" />'+
'</div>';
$(field_to_append).insertAfter('#form-field-enquiry_source');
} else {
$('#form-field-enquiry_source_other').remove();
}
});
});
任何想法是什么导致了这个问题?