我认为我的问题很简单(但我确实很简单),我没有使用jquery或任何其他插件,而只是使用纯JavaScript和PhP。我有一个带有选择字段的简单表单,可以在其中选择多个项目,例如:
<form id="test" name="test">
<input type="hidden" id="norman" value="bates"/>
<select multiple="multiple" name="fred[]" id="fred[]">
<option value="1">one</option>
<option value="2">two</option>
<option value="3">three</option>
<option value="4">four</option>
</select>
<input type="button" id="button" value="test" onclick="callAHAH('POST','my-page.php','container','loading...','modify-user')"/>
callAHAH是javaScript函数,用于包装Ajax的数据,该函数是:
function callAHAH(method,url, pageElement, callMessage,form_name){
//window.location = url;
//document.getElementById(pageElement).innerHTML = callMessage;
try{
req = new XMLHttpRequest(); /* ff */
}
catch(e){
try{
req = new ActiveObject("Msxml2.XMLHTTP"); /* some ie */
}
catch(e){
try{
req = new ActiveXObject("Microsoft.XMLHTTP"); /*other ie */
}
catch(e){
req = false;
}
}
}
req.onreadystatechange = function(){responseAHAH(pageElement);};
var build_url=window.location.origin;
var url = "aj_scripts/"+url;
if(build_url=="http://localhost:8080"){
url=build_url+"/pitd_outer/pitd/"+url;
}
// Check request status
if(method=='POST'){
req.open("POST",url,true);
// adds a header to tell the PHP script to recognize the data as is sent via POST
req.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
var the_data="";
///
var the_form=document.getElementById(form_name);
//alert(form_name);
for (var i=0;i<the_form.length;i++){
var the_type = the_form.elements[i].type;
var value;
if(the_type !="button"){
if(the_type =="checkbox"){
value = the_form.elements[i].checked
// alert("the array name is: "+the_array_name+" and it is checked:"+checked);
}else{
value=the_form.elements[i].value;
}
var id= the_form.elements[i].id;
the_data+=id;
the_data+="="+value+"&";
}
}
the_data=the_data.substring(0,the_data.length-1);//removing the last & symbol
// alert(the_data);
req.send(the_data); // calls the send() method with datas as parameter
}else if(method=="GET"){
req.open("GET",url,true);
req.send(null);
}
}
JS处理程序一直运行良好,直到我尝试选择多个项目,然后代码仅返回第一个或最后一个选定的项目。
我知道表单正在发送一个数组,但是我似乎无法获得代码来正确测试我尝试过的数组
var array_test=Array.isArray(the_form.elements[i]);
alert("array test:"+array_test);
但是我得到的都是假的,所以...
如何格式化帖子的文本字符串
my_array[]=1$my_array[]=2 etc
在此先感谢您的帮助
禅
好的,我有答案!
function callAHAH(method,url, pageElement, callMessage,form_name){
//window.location = url;
//document.getElementById(pageElement).innerHTML = callMessage;
try{
req = new XMLHttpRequest(); /* ff */
}
catch(e){
try{
req = new ActiveObject("Msxml2.XMLHTTP"); /* some ie */
}
catch(e){
try{
req = new ActiveXObject("Microsoft.XMLHTTP"); /*other ie */
}
catch(e){
req = false;
}
}
}
req.onreadystatechange = function(){responseAHAH(pageElement);};
var build_url=window.location.origin;
var url = "aj_scripts/"+url;
if(build_url=="http://localhost:8080"){
url=build_url+"/pitd_outer/pitd/"+url;
}
// Check request status
if(method=='POST'){
req.open("POST",url,true);
// adds a header to tell the PHP script to recognize the data as is sent via POST
req.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
var the_data="";
///
var the_form=document.getElementById(form_name);
//alert(form_name);
for (var i=0;i<the_form.length;i++){
var the_type = the_form.elements[i].type;
var value;
if(the_type !="button"){
if(the_type =="checkbox"){
value = the_form.elements[i].checked
var id= the_form.elements[i].id;
the_data+=id;
the_data+="="+value+"&";
}else if(the_form.elements[i].hasAttribute('multiple') == true){
var test_data = "";
var the_multiples = the_form.elements[i].options;
if((the_form.elements[i].hasAttribute('getall')==true)&&(the_form.elements[i].getAttribute('getall')=="get_all")){
//a multiple select that we need to get all values from and not just those that are selected
alert("hi");
console.log("inside the_form_elements has attributes");
for(var j=0;j<the_multiples.length;j++){
test_data += the_form.elements[i].id+"="+the_multiples[j].value+"&";
}//end of for var j
}else{
//a select list with potentially multiple selections and only want the selected ones
for(var j=0;j<the_multiples.length;j++){
if(the_multiples[j].selected == true){
test_data += the_form.elements[i].id+"="+the_multiples[j].value+"&";
}//end of if the_multiples
}//end of for var j
}//end of the if the_form inner
test_data=test_data.substring(0,test_data.length-1);//removing the last & symbol
the_data +=test_data;
alert(test_data);
}else{
value=the_form.elements[i].value;
var id= the_form.elements[i].id;
the_data+=id;
the_data+="="+value+"&";
}//end of if the_form outer
}
}
the_data=the_data.substring(0,the_data.length-1);//removing the last & symbol
// alert(the_data);
req.send(the_data); // calls the send() method with datas as parameter
}else if(method=="GET"){
req.open("GET",url,true);
req.send(null);
}
}
function responseAHAH(pageElement){
var output='';
if(req.readyState == 4){
if(req.status == 200){
output = req.responseText;
document.getElementById(pageElement).innerHTML = output;
}
}
}
该代码的重要部分是:
if(the_type !="button"){
if(the_type =="checkbox"){
value = the_form.elements[i].checked
var id= the_form.elements[i].id;
the_data+=id;
the_data+="="+value+"&";
}else if(the_form.elements[i].hasAttribute('multiple') == true){
var test_data = "";
var the_multiples = the_form.elements[i].options;
if((the_form.elements[i].hasAttribute('getall')==true)&&(the_form.elements[i].getAttribute('getall')=="get_all")){
//a multiple select that we need to get all values from and not just those that are selected
alert("hi");
console.log("inside the_form_elements has attributes");
for(var j=0;j<the_multiples.length;j++){
test_data += the_form.elements[i].id+"="+the_multiples[j].value+"&";
}//end of for var j
}else{
//a select list with potentially multiple selections and only want the selected ones
for(var j=0;j<the_multiples.length;j++){
if(the_multiples[j].selected == true){
test_data += the_form.elements[i].id+"="+the_multiples[j].value+"&";
}//end of if the_multiples
}//end of for var j
}//end of the if the_form inner
test_data=test_data.substring(0,test_data.length-1);//removing the last & symbol
the_data +=test_data;
alert(test_data);
}else{
value=the_form.elements[i].value;
var id= the_form.elements[i].id;
the_data+=id;
the_data+="="+value+"&";
}//end of if the_form outer
从本质上讲,我需要起诉JavaScript hasAttribute函数,尽管我知道它并不是每个浏览器都支持,但是它可以在IE10以及FF O和C的最新版本上运行。
从那里,我必须使用.options子选择在每个选择选项中循环查找所选择的选项。
那些检查代码的人会注意到我那里有一个怪异的条件,hasAttribute('getall'),因为我有一个非常怪异的情况,我需要从多重选择中获取所有值,而不管它们是否已经被再次选择,我知道并非所有浏览器都支持非标准属性,但是我使用的是非标准属性。
问候
禅
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句