Answers
封装json需要自行拼接。Google: javascript form to json 会有很多结果。
更长见的组织方式为getstring。详见jQuery的.serialize()方法。
$( "form" ).on( "submit", function( event ) {
event.preventDefault();
console.log( $( this ).serialize() );
});
e.g.
<form>
<select name="single">
<option>Single</option>
<option>Single2</option>
</select>
<br>
<select name="multiple" multiple="multiple">
<option selected="selected">Multiple</option>
<option>Multiple2</option>
<option selected="selected">Multiple3</option>
</select>
<br>
<input type="checkbox" name="check" value="check1" id="ch1">
<label for="ch1">check1</label>
<input type="checkbox" name="check" value="check2" checked="checked" id="ch2">
<label for="ch2">check2</label>
<br>
<input type="radio" name="radio" value="radio1" checked="checked" id="r1">
<label for="r1">radio1</label>
<input type="radio" name="radio" value="radio2" id="r2">
<label for="r2">radio2</label>
</form>
<p><tt id="results"></tt></p>
<script>
function showValues() {
var str = $( "form" ).serialize();
$( "#results" ).text( str );
}
$( "input[type='checkbox'], input[type='radio']" ).on( "click", showValues );
$( "select" ).on( "change", showValues );
showValues();
</script>
//Output
single=Single&multiple=Multiple&multiple=Multiple3&check=check2&radio=radio1
老衲咬杀你
answered 10 years, 6 months ago
题主问的是
但是在python,不知道该怎么把数据封装为json格式
,我理解也就是在后台怎么用python解析和封装json。给个栗子:
# -*- coding: utf-8 -*-
import json
#封装json
data=[]
for i in range(5):
tmp = {}
tmp['sex'] = 'boy_%s' % i
tmp['declare'] = 'apple_%s' % i
data.append(tmp)
json_str = json.dumps(data)
print type(json_str)
#解析json
json_data = json.loads(json_str)
print json_data[1]['sex']
输出:
<type 'str'>
boy_1
这其实在网上一查就能查到。包括前端获取每个文本框的数据组装成json都可以找到现成的资料。
鋼鉄D加鲁鲁
answered 10 years, 6 months ago