次のコードがあります。ユーザーがドロップダウンから[その他]を選択すると、フォームにフィールドが追加されます。ただし、このコードを使用すると、ドロップダウンの値が上書きされるため、フォームが投稿されたときに$_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();
}
});
});
この問題の原因は何ですか?