我正在尝试将用户选择的<option>
附加到发送给php页面的ajax调用中。
(我对ajax或jQuery不是很熟悉)
我建立了一个JSFiddle,希望它比我能解释的更容易理解。这是它的要点:
HTML
<select name="internType" style="width: 100%; display: block; color: #000;" id="internType">
<option selected value="default">Please Select</option>
<option value="accounting">Accounting</option>
<option value="auditing">Auditing</option>
<option value="finance">Finance</option>
<option value="humanresources">Human Resources</option>
</select>
jQuery
formData = new FormData( $(this)[0] );
self.sendData(formData);
sendData : function(formData){
var self = this;
var internChoice = $('#internType').val() // This doesn't seem to hold the value
formData.append('internChoice', internChoice); // but rather the entire list
console.log(formData);
$.ajax({
url: self.settings.path + '?i=' + self.settings.app ,
type: 'POST',
data: formData,
async: false,
success: function (data) {
console.log(data);
outcome = jQuery.parseJSON( data );
self.handleOutcome(outcome);
},
cache: false,
contentType: false,
processData: false
});
},
当我执行 var_dump($_POST);
时,我会收到从输入框传递过来的数据,但是从下拉菜单中我会收到一个包含 所有 值的字符串。例如:
["internSelect"]=>
string(130) "
Please Select
Accounting
Auditing
Finance
Human Resources
"
我做错了什么,如何更好地排除错误?
我是否失去了选定的值?页面从未刷新,所以我不确定这可能是什么原因。
我正在使用jQuery 1.10.2,如果需要知道的话。