jQuery SerializeArray() JSON 字符串

标签 jquery string json serializearray

我正在尝试在 jQuery 中序列化我的表单提交。我正在尝试获取类似的 JSON 字符串或对象。另外,如果有人能让我知道如何只选择那些有值的小部件而不是空的小部件,那就完美了。

我很匆忙,因此没有检查语法,对此我深表歉意。

   <html>
    <head>
    <script type="text/javascript">

    $(document.ready(function(){
       $("#myform").submit(function(){

           var mySerialObj = $("#myform").serializeArray();

            $.each(mySerialObj,function(indx,idxVal){
                 //here indx is numeric and idxVal is a String like
                 // [{{"name","name"},{"value","RED"}}]

                     $.each(JSON.parse(idxVal),function(i,v){

                           //here I am not able to get the thinggy into a 
                           //  JSON format something like ['name','RED'] 
                        });
});

});


});   


    </script>
    </head>
    <body>

    <form id="myform">
    <div>
    <span>What color do you prefer?</span><br />
    <input type="radio" name="colors" id="red" />Red<br />
    <input type="radio" name="colors" id="blue" />Blue<br />
    <input type="radio" name="colors" id="green" />Green
    </div>

    <div> 
    <select>
      <option value="volvo">Volvo</option>
      <option value="saab">Saab</option>
      <option value="mercedes">Mercedes</option>
      <option value="audi">Audi</option>
    </select>
    </div>

    </form>

    <button type="submit" value="submit" id="sbmt"">submit</button>


    </body>
    </html>

最佳答案

只需使用:

$("#myform").serialize();

You can try it here.

关于jQuery SerializeArray() JSON 字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5467206/

相关文章:

mysql - 在 Go REST-API 中传输 MySQL JSON 数据类型

javascript - 如何用 Javascript 替换文本区域中选定的文本?

c# - C# (.NET 4.0) 中的快速字符串后缀检查?

javascript - 我可以将 jQuery DOM 克隆存储在本地存储中以供以后使用吗?

python - 将数字转换为字符串

java - 比较和更新字符串

javascript - 在asp.net mvc中将json数据渲染到网格中

php - Symfony2 响应内容必须是字符串或对象实现 __toString(), "boolean"given

javascript - 又是Safari??表单提交绕过 AJAX

jquery - 取得回应式YouTube影片的高度