哪大侠能帮忙用JS写下getarr()函数么
<select multiple="multiple" name="fromBox" id="fromBox" >
<option value="1">111</option>
<option value="2">222</option>
<option value="3">333</option>
</select>
<input type=text name='aaa' id='aaa'>
<input type='button' onclick=getarr()>
点击后获取select中所有值和select选中值,加上aaa值所组成的json格式的数据么。
<select multiple="multiple" name="fromBox" id="fromBox" >
<option value="1">111</option>
<option value="2">222</option>
<option value="3">333</option>
</select>
<input type=text name='aaa' id='aaa'>
<input type='button' onclick=getarr()>
点击后获取select中所有值和select选中值,加上aaa值所组成的json格式的数据么。
<option value="1">111</option>
<option value="2">222</option>
<option value="3">333</option>
</select>
$json='[{"1":"111","2":"222","3":"333"},{"selected":"222"},{"aaa":"value"}]';
var_dump(json_decode($json,true));
?>array
0 =>
array
1 => string '111' (length=3)
2 => string '222' (length=3)
3 => string '333' (length=3)
1 =>
array
'selected' => string '222' (length=3)
2 =>
array
'aaa' => string 'value' (length=5)
/**
* formToArray() gathers form element data into an array of objects that can
* be passed to any of the following ajax functions: $.get, $.post, or load.
* Each object in the array has both a 'name' and 'value' property. An example of
* an array for a simple login form might be:
*
* [ { name: 'username', value: 'jresig' }, { name: 'password', value: 'secret' } ]
*
* It is this array that is passed to pre-submit callback functions provided to the
* ajaxSubmit() and ajaxForm() methods.
*/
$.fn.formToArray = function(semantic) {
var a = [];
if (this.length == 0) return a; var form = this[0];
var els = semantic ? form.getElementsByTagName('*') : form.elements;
if (!els) return a;
for(var i=0, max=els.length; i < max; i++) {
var el = els[i];
var n = el.name;
if (!n) continue; if (semantic && form.clk && el.type == "image") {
// handle image inputs on the fly when semantic == true
if(!el.disabled && form.clk == el) {
a.push({name: n, value: $(el).val()});
a.push({name: n+'.x', value: form.clk_x}, {name: n+'.y', value: form.clk_y});
}
continue;
} var v = $.fieldValue(el, true);
if (v && v.constructor == Array) {
for(var j=0, jmax=v.length; j < jmax; j++)
a.push({name: n, value: v[j]});
}
else if (v !== null && typeof v != 'undefined')
a.push({name: n, value: v});
} if (!semantic && form.clk) {
// input type=='image' are not found in elements array! handle it here
var $input = $(form.clk), input = $input[0], n = input.name;
if (n && !input.disabled && input.type == 'image') {
a.push({name: n, value: $input.val()});
a.push({name: n+'.x', value: form.clk_x}, {name: n+'.y', value: form.clk_y});
}
}
return a;
};/**
* Serializes form data into a 'submittable' string. This method will return a string
* in the format: name1=value1&name2=value2
*/
$.fn.formSerialize = function(semantic) {
//hand off to jQuery.param for proper encoding
return $.param(this.formToArray(semantic));
};