Jquery ajax 和每个变量?
Jquery ajax and for each variable?
在javascript这很容易工作,wordpress需要jqueryajax,而我对jquery不是最熟悉的。以下示例是否可以使用 ajax jquery?
如何发送多个变量,使“var notei++”增加一个数,发送为:
note1 = "My note 1",
note2 = "My note 2",
note3 = "My note 3",
note4 = "My note 4",
还有更多......
var i = "";
var note = "";
var divsname = document.getElementsByClassName("notes");
for (i = 1, i++) {
divsname.forEach(function(div) {
note += note i++ = div.innerHTML;
});
jQuery(document).ready(function($) {
var data = {
'action': 'my_actione',
'user_id': '1',
note
};
// since 2.8 ajaxurl is always defined in the admin header and points to admin-ajax.php
jQuery.post(ajaxurl, data, function(response) {
alert('Got this from the server: ' + response);
});
});
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="notes">My note 1</div>
<div class="notes">My note 2</div>
<div class="notes">My note 3</div>
<div class="notes">My note 4</div>
您可以使用 JSON 数组 发送您的 div 数据,索引号和值是 div 中的文本,然后通过与您的 ajax.
相同
演示代码 :
var divsname = $(".notes");
notes = []
divsname.each(function(i) {
//push in json array
notes.push({
[`note${i + 1}`]: $(this).text()
})
});
var data = {
'action': 'my_actione',
'user_id': '1',
'note': notes //pass same
};
console.log(data)
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="notes">My note 1</div>
<div class="notes">My note 2</div>
<div class="notes">My note 3</div>
<div class="notes">My note 4</div>
在javascript这很容易工作,wordpress需要jqueryajax,而我对jquery不是最熟悉的。以下示例是否可以使用 ajax jquery?
如何发送多个变量,使“var notei++”增加一个数,发送为:
note1 = "My note 1",
note2 = "My note 2",
note3 = "My note 3",
note4 = "My note 4",
还有更多......
var i = "";
var note = "";
var divsname = document.getElementsByClassName("notes");
for (i = 1, i++) {
divsname.forEach(function(div) {
note += note i++ = div.innerHTML;
});
jQuery(document).ready(function($) {
var data = {
'action': 'my_actione',
'user_id': '1',
note
};
// since 2.8 ajaxurl is always defined in the admin header and points to admin-ajax.php
jQuery.post(ajaxurl, data, function(response) {
alert('Got this from the server: ' + response);
});
});
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="notes">My note 1</div>
<div class="notes">My note 2</div>
<div class="notes">My note 3</div>
<div class="notes">My note 4</div>
您可以使用 JSON 数组 发送您的 div 数据,索引号和值是 div 中的文本,然后通过与您的 ajax.
相同演示代码 :
var divsname = $(".notes");
notes = []
divsname.each(function(i) {
//push in json array
notes.push({
[`note${i + 1}`]: $(this).text()
})
});
var data = {
'action': 'my_actione',
'user_id': '1',
'note': notes //pass same
};
console.log(data)
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="notes">My note 1</div>
<div class="notes">My note 2</div>
<div class="notes">My note 3</div>
<div class="notes">My note 4</div>