如何使chechbox的json字符串检查div文本

时间:2016-04-15 19:37:16

标签: javascript jquery json

如何在按钮点击时使用jquery制作json字符串,但文本应该是chechbox选中的。

这些是动态创建的div

email = request("emailPost")
firstName = request("firstNamePost")
lastName = request("lastNamePost")
points = request("pointsPost")
id = request("idPost")
sql = "INSERT INTO databaseName.tableName (email, firstName, lastName, points, id) VALUES (" &_
   "'" & email & "', " &_ 
   "'" & firstName & "', " &_ 
   "'" & lastName  & "', " &_ 
   "'" & points & "', " &_ 
   "'" & id & "')"
ADODBConn.Execute sql


这是欲望输出想要的textarea

<div class="div1">
    <input type="checkbox" class="checkbox" value="1">
    <div class="divdata">divtext1</div>
    <span class="spandata">spantext1</span>
</div>
<div class="div1">
    <input type="checkbox" class="checkbox" value="1">
    <div class="divdata">divtext2</div>
    <span class="spandata">spantext2</span>
</div>
<div class="div1">
    <input type="checkbox" class="checkbox" value="1">
    <div class="divdata">divtext3</div>
    <span class="spandata">spantext3</span>
</div>
<input type="submit" id="btndisplay" value="Display Data">


jQuery代码

<textarea>
    {
        "mydata":[
            {"divdata":"divtext1", "spandata":"spantext1"}, 
            {"divdata":"divtext2", "spandata":"spantext2"}, 
            {"divdata":"divtext3", "spandata":"spantext3"}
        ]
    }
</textarea>

1 个答案:

答案 0 :(得分:1)

我相信你想要的就是这些。

JS

$('#btndisplay').click(function(){
  var obj = {
    "myData": []
  };

  $('.div1').each(function() {
    if($(this).find('.checkbox').is(':checked')) {

      obj["myData"].push({
        'divdata': $(this).find('.divdata').text(),
        'spandata': $(this).find('.spandata').text()
      });

    }
  });

  $('textarea').val(JSON.stringify(obj));
});

见js小提琴 https://jsfiddle.net/23pf5t10/4/