![](/img/trans.png)
[英]jquery json http post - How to convert the post data to json format?
[英]How to convert Div data into json format using jquery
這是我的 html 代碼。 我希望在單擊導出按鈕時調用此 div id“con”,然后在控制台中顯示 json 數據。
如果有人知道請幫忙
<html>
<div id ="con">
<div>Name : P</div>
<div>Age : 20</div>
</div>
<button type="submit" onclick="append()">Export</button>
</html>
您可以嘗試以下方法,我已將class
屬性添加到您的按鈕:
$('.conSubmit').on('click', function() { var JSONObj = {}; var divArr = $('#con div'); $.each(divArr, function(index, val) { var data = val.innerText; var dataArray = data.split(':'); index == 0 ? JSONObj.name = dataArray[1].trim() : JSONObj.Age = dataArray[1].trim(); }) console.log(JSONObj); })
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div id="con"> <div>Name : P</div> <div>Age : 20</div> </div> <button class="conSubmit" type="submit">Export</button>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.