Javascript 正在尝试将变量状态传递给服务器
因此,我试图将一个变量布尔“state”(true/false)从HTML文件传递给服务器Javascript 正在尝试将变量状态传递给服务器,javascript,jquery,html,Javascript,Jquery,Html,因此,我试图将一个变量布尔“state”(true/false)从HTML文件传递给服务器 <script> $("#aaa").click(function () { $(this).hide('slow'); $("#bam").html(shay + " arrived ").hide().show(700); childName = true; counter++; if (counter >
<script>
$("#aaa").click(function () {
$(this).hide('slow');
$("#bam").html(shay + " arrived ").hide().show(700);
childName = true;
counter++;
if (counter > 12) {
$("#bam").html("All have arrived").hide().show(700);
}
});
</script>
$(“#aaa”)。单击(函数(){
$(this.hide('slow');
$(“#bam”).html(shay+“到达”).hide().show(700);
childName=true;
计数器++;
如果(计数器>12){
$(“#bam”).html(“全部已到达”).hide().show(700);
}
});
对于每个上学的孩子,我有多个此函数的实例。单击孩子的名字时,孩子变量的名字等于true。我想做的是将这些变量的布尔状态发送到服务器,这样我就能远程知道每个孩子的“状态”。有没有办法用jQuery的Post方法来实现这一点?如果不是,最简单的方法是什么?谢谢 是的,你能行
<script>
$("#aaa").click(function () {
$(this).hide('slow');
$("#bam").html(shay + " arrived ").hide().show(700);
childName = true;
$.post("url",
{
childStatus: childName,
},
function(status){ // return from server
});
counter++;
if (counter > 12) {
$("#bam").html("All have arrived").hide().show(700);
}
});
</script>
$(“#aaa”)。单击(函数(){
$(this.hide('slow');
$(“#bam”).html(shay+“到达”).hide().show(700);
childName=true;
$.post(“url”,
{
childStatus:childName,
},
函数(状态){//从服务器返回
});
计数器++;
如果(计数器>12){
$(“#bam”).html(“全部已到达”).hide().show(700);
}
});
是的,你可以做
<script>
$("#aaa").click(function () {
$(this).hide('slow');
$("#bam").html(shay + " arrived ").hide().show(700);
childName = true;
$.post("url",
{
childStatus: childName,
},
function(status){ // return from server
});
counter++;
if (counter > 12) {
$("#bam").html("All have arrived").hide().show(700);
}
});
</script>
$(“#aaa”)。单击(函数(){
$(this.hide('slow');
$(“#bam”).html(shay+“到达”).hide().show(700);
childName=true;
$.post(“url”,
{
childStatus:childName,
},
函数(状态){//从服务器返回
});
计数器++;
如果(计数器>12){
$(“#bam”).html(“全部已到达”).hide().show(700);
}
});
您可以使用jquery post方法将任何内容发布到服务器,如下面的示例:
$("#aaa").click(function () {
$(this).hide('slow');
$("#bam").html(shay + " arrived ").hide().show(700);
childName = true;
counter++;
$.post("destination.asp", {childStatus: childName}, function(result){
// do any thing
});
if (counter > 12) {
$("#bam").html("All have arrived").hide().show(700);
}
});
您可以使用jquery post方法将任何内容发布到服务器,如下面的示例:
$("#aaa").click(function () {
$(this).hide('slow');
$("#bam").html(shay + " arrived ").hide().show(700);
childName = true;
counter++;
$.post("destination.asp", {childStatus: childName}, function(result){
// do any thing
});
if (counter > 12) {
$("#bam").html("All have arrived").hide().show(700);
}
});
每个孩子都可以有一个id。并添加一个向服务器发送数据的事件侦听器。比如说, HTML:
<button id="id_001" class="childBtn">Arrived</div>
<button id="id_002" class="childBtn">Arrived</div>
<button id="id_003" class="childBtn">Arrived</div>
$(".childBtn").click(function(){
$.post('http://yourserver.com/api', { childId : $(this).attr('id')},
function(data, status) {
//alert("Data: " + data + "\nStatus: " + status);
}
});
我希望这有帮助:)你可以为每个孩子都有一个id。并添加一个向服务器发送数据的事件侦听器。比如说, HTML:
<button id="id_001" class="childBtn">Arrived</div>
<button id="id_002" class="childBtn">Arrived</div>
<button id="id_003" class="childBtn">Arrived</div>
$(".childBtn").click(function(){
$.post('http://yourserver.com/api', { childId : $(this).attr('id')},
function(data, status) {
//alert("Data: " + data + "\nStatus: " + status);
}
});
我希望这有帮助:)是的,请查看.ajax()的文档: 可能看起来像这样:
$.ajax({
type: 'POST',
url: 'your-endpoint-url', // the endpoint for the call
data: childName,
success: function(response){
console.log('AJAX success: ' + response);
// maybe do something else here ...
},
error: function(error, status, xhr){
console.log('AJAX error: ' + error);
console.log('STATUS: ' + status + ', XHR: ' + xhr);
}
});
旁注:你说你有这个函数的多个实例;那么,为什么要将click函数分配给DOM元素(“#aaa”),而不是通过(例如)类选择器(给schookid行一个css类“.schoolkid”并通过类选择器分配click函数)是的,请查看.ajax()的文档: 可能看起来像这样:
$.ajax({
type: 'POST',
url: 'your-endpoint-url', // the endpoint for the call
data: childName,
success: function(response){
console.log('AJAX success: ' + response);
// maybe do something else here ...
},
error: function(error, status, xhr){
console.log('AJAX error: ' + error);
console.log('STATUS: ' + status + ', XHR: ' + xhr);
}
});
旁注:你说你有这个函数的多个实例;那么为什么要将click函数分配给DOM元素(“#aaa”),而不是通过(例如)类选择器(给schookid行一个css类“.schoolkid”并通过类选择器分配click函数)是的,jQuery.post将是将数据发布到服务器的正确方式是的,您可以通过这种方式将数据发布到服务器,但是你在接收数据的服务器上有后端吗?是的,我正在使用我的大学服务器。是的,jQuery.post将是将数据发布到服务器的正确方法。是的,你可以通过这种方式将数据发布到服务器,但你甚至在接收数据的服务器上有后端吗?是的,我正在使用我的大学服务器。