jQuery中serialize()怎么用

发布时间:2022-03-05 11:21:24 作者:小新
来源:亿速云 阅读:119

这篇文章给大家分享的是有关jQuery中serialize()怎么用的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。

  jQuery成功的控制表单的序列化。只有form元素检查它们所包含的输入框,在所有其他情况下,输入元素要序列化应该是集合的一部分传递给.serialize()方法。选择集合中表单和它子元素在序列化的字符串会导致重复。

  注意: 只有 "successful controls"可以被序列化成字符串。其中,提交按钮的值不会被序列化。另外,如果想要一个表单元素的值被序列化成字符串,这个元素必须含有 name 属性。此外,复选框(checkbox)和单选按钮(radio)(input 类型为 "radio" 或 "checkbox")的值只有在被选中时才会被序列化。另外,文件选择元素的数据也不会被序列化。

  例子:

  把一个表单序列化成一个查询字符串,使之能够在一个Ajax请求中发送。

  <!DOCTYPE html>

  <html>

  <head>

  <style>

  body, select { font-size:12px; }

  form { margin:5px; }

  p { color:red; margin:5px; font-size:14px; }

  b { color:blue; }

  </style>

  <script src="https://code.jquery.com/jquery-latest.js"></script>

  </head>

  <body>

  <form>

  <select name="single">

  <option>Single</option>

  <option>Single2</option>

  </select>

  <br />

  <select name="multiple" multiple="multiple">

  <option selected="selected">Multiple</option>

  <option>Multiple2</option>

  <option selected="selected">Multiple3</option>

  </select>

  <br/>

  <input type="checkbox" name="check" value="check1" id="ch2"/>

  <label for="ch2">check1</label>

  <input type="checkbox" name="check" value="check2" checked="checked" id="ch3"/>

  <label for="ch3">check2</label>

  <br />

  <input type="radio" name="radio" value="radio1" checked="checked" id="r1"/>

  <label for="r1">radio1</label>

  <input type="radio" name="radio" value="radio2" id="r2"/>

  <label for="r2">radio2</label>

  </form>

  <p><tt id="results"></tt></p>

  <script>

  function showValues() {

  var str = $("form").serialize();

  $("#results").text(str);

  }

  $(":checkbox, :radio").click(showValues);

  $("select").change(showValues);

  showValues();

  </script>

  </body>

  </html>

感谢各位的阅读!关于“jQuery中serialize()怎么用”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,让大家可以学到更多知识,如果觉得文章不错,可以把它分享出去让更多的人看到吧!

推荐阅读:
  1. Android开发框架之---ThinkAndroid
  2. Error: ShouldNotReachHere()(Android)

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

serialize() jquery

上一篇:jQuery中$.ajax()提供的回调钩子是什么

下一篇:如何使用callbacks.remove()

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》