序列化元素内部的输入(不是表单)

时间:2017-11-13 19:05:28

标签: javascript jquery json serialization

我有一个HTML:

<table>
    <tr id="01">
        <td><input name="x"></td>
        <td><input name="y"></td>
    </tr>
    <tr id="02">
        <td><input name="x"></td>
        <td><input name="y"></td>
    </tr>
</table>

我正在尝试仅在特定行上创建输入的JSON字符串。会有一个很好的解决方案吗?类似的东西:

json = $('#01').serialize();

但我需要一个json而不是serialize()提供的URLEncode'd字符串。

3 个答案:

答案 0 :(得分:1)

您可以使用$("#01 input").serialize()

$('#abc').click(function(){
    json = $("#01 input").serialize().replace(/%20/g, "+");
    console.log(json);
});
<table>
    <tr id="01">
        <td><input name="x"></td>
        <td><input name="y"></td>
    </tr>
    <tr id="02">
        <td><input name="x"></td>
        <td><input name="y"></td>
    </tr>
</table>
<button id = "abc">serialize 01</button>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>

答案 1 :(得分:1)

persons.removeIf(x -> ids.contains(x.getId())) 可用于输入集合。

&#13;
&#13;
.serialize()
&#13;
$("#submit").click(function() {
  var data = $("#01 input").serialize();
  console.log(data);
});
&#13;
&#13;
&#13;

顺便说一句,<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <table> <tr id="01"> <td><input name="x"></td> <td><input name="y"></td> </tr> <tr id="02"> <td><input name="x"></td> <td><input name="y"></td> </tr> </table> <button id="submit">Click</button>的结果不是JSON,它是serialize()格式。

答案 2 :(得分:0)

serialize()不会为您提供您之后的JSON。它只会给你一个URLEncode&#39; d字符串。例如:

&#13;
&#13;
//test all inputs on row #02
window.tester = function() { 
	console.log($('#02 input').serialize());
};

//test all inputs
window.tester2 = function() { 
	console.log($('input').serialize());
};

//test only "y" inputs
window.tester3 = function() { 
	console.log($("input[name='y']").serialize());
};
&#13;
<table>
    <tr id="01">
        <td><input name="x" value="1"></td>
        <td><input name="y" value="2"></td>
    </tr>
    <tr id="02">
        <td><input name="x" value="3"></td>
        <td><input name="y" value="4"></td>
    </tr>
</table>
<div>
  <input type="button" onclick="tester()" value="Test Row 02" /><br />
  <input type="button" onclick="tester2()" value="Test All Inputs" /><br />
  <input type="button" onclick="tester3()" value="Test Only 'y' Inputs" />
</div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.2.4/jquery.min.js"></script>
&#13;
&#13;
&#13;

产地:

x=3&y=4
x=1&y=2&x=3&y=4
y=2&y=4

如果您需要JSON,那么您需要具体说明您之后使用的格式。数组?哈希?在任何情况下,上面的选择器都是您想要开始的,然后在某些时候,您将要通过以下方式运行该值:

JSON.stringify(myResults); 

&#13;
&#13;
window.arrayIt = function () {
  var arrVal = $('#02 input').toArray();
  var arr = arrVal.map(function (i) {
    var x = {};
    x[i.name] = i.value;
    return x;
  });
  console.log(JSON.stringify(arr));
}
&#13;
<table>
    <tr id="01">
        <td><input name="x" value="1"></td>
        <td><input name="y" value="2"></td>
    </tr>
    <tr id="02">
        <td><input name="x" value="3"></td>
        <td><input name="y" value="4"></td>
    </tr>
</table>
<button onclick="arrayIt()">Array It!</button>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.2.4/jquery.min.js"></script>
&#13;
&#13;
&#13;