Jquery在form表單利用submit取得所有的inputs
按下submit後將表格內的所有input讀取出並轉成json格式
示意表格(簡化):
<form name="demo-form" id="test">
<div>
<label>Full Name :</label>
<input name="fullname" />
</div>
<div>
<label>Email :</label>
<input name="email" />
</div>
<div>
<label>Address :</label>
<input name="address" />
</div>
<div>
<label>Phone :</label>
<input name="phone" />
</div>
<div>
<label>Fax:</label>
<input name="fax" />
</div>
<div>
<button type="submit"></button>
</div>
</form>
Jquery code:
$( "#test" ).submit(function(){
var $inputs = $('#test :input').not(':button'); //如果只有input會抓到button的值
var values = {};
$inputs.each(function() {
values[this.name] = $(this).val();
});
var params=JSON.stringify(values);
console.log(params);
});
參考連結:
http://stackoverflow.com/questions/169506/obtain-form-input-fields-using-jquery
留言
張貼留言