jquery中的 ajax 以及map遍歷


1.語法

 $.ajax{

  type:'get',//類型 有get post

  url:'',//路徑

  data:{name:$('#ma').val(),nameq:$('#maq').val()},

  dataType="json",

  success:function(data){alert("成功了")}

 }

2.獲取data數據的方法

如果這樣寫的話會寫吐血的name:$('#ma').val(),nameq:$('#maq').val()

可以用map方法

var map=$(':checkbox').map(function() {
  return ($(this).attr("id")+'='+$(this).val());
}).get().join('&');
alert(map)

<form method="post" action="">
  <fieldset>
    <div>
      <label for="two">2</label>
      <input type="checkbox" value="2" id="two" name="number[]">
    </div>
    <div>
      <label for="four">4</label>
      <input type="checkbox" value="4" id="four" name="number[]">
    </div>
    <div>
      <label for="six">6</label>
      <input type="checkbox" value="6" id="six" name="number[]">
    </div>
    <div>
      <label for="eight">8</label>
      <input type="checkbox" value="8" id="eight" name="number[]">
    </div>
  </fieldset>
</form>

</body>
<script>
  var map=$(':checkbox').map(function() {
    return ($(this).attr("id")+'='+$(this).val());
  }).get().join('&');
  alert(map)
</script>

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM