我需要访问PUT请求中的预留器以发送到url / datas。谁知道怎么做?
<head>
<meta charset="UTF-8">
<title><%= title %></title>
<link rel="stylesheet" href="bootstrap/dist/css/bootstrap.css">
<script src="//ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
</head>
<style type="text/css">
.starter-template {
margin-top: 150px;
}
.checkbox {
width: 40px;
}
</style>
<body>
<div class="navbar navbar-inverse navbar-fixed-top" role="navigation">
<div class="container">
<div class="navbar-header">
<button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target=".navbar- collapse">
<span class="sr-only">Toggle navigation</span>
</button>
<a class="navbar-brand" href="#">Banco de dados</a>
</div>
<div class="collapse navbar-collapse">
<ul class="nav navbar-nav">
<li class="active"><a href="/">Cadastros</a></li>
<li><a href="datas">Datas</a></li>
</ul>
</div> <!--/.nav-collapse -->
</div>
</div>
<div class="container">
<div class="starter-template">
<table class="table">
<thead>
<tr>
<th>✓</th>
<th>ID</th>
<th>Nome</th>
<th>Email</th>
</tr>
<div id="checkbox"></div>
</thead>
<tbody>
</tbody>
</table>
</div>
</div>
<script>
$.ajax({
type: 'GET',
//async: false,
url: 'https://app.myurl.com/reservas',
success:function(reservas){
reservas.forEach (function (reserva) {
var reservas = [];
reservas.push('<tr class="reservas">');
reservas.push('<td> <input type="checkbox" id="salvar"/> </td>');
reservas.push('<td>' + reserva._id + '</td>');
reservas.push('<td>' + reserva.nome + '</td>');
reservas.push('<td>' + reserva.email + '</td>');
reservas.push('</tr>');
$('tbody').append(reservas.join(""));
})
},
error:function(e){
console.log(e);
}
});
他们在点击事件中有PUT请求。
$(function(){
$('body').on('click', '#salvar', function(){
alert('Salvo!');
$.ajax({
type: 'PUT',
url: 'http://localhost/datas',
//async: false,
data: {
id: reserva._id,
nome: reserva.nome,
email: reserva.email
},
sucess:function (success) {
console.log('success')
},
error:function(err) {
console.log(err);
}
});
});
});
</script>
如果你可以指导我,我很感激=)
非常感谢。
答案 0 :(得分:0)
您不需要全局变量,因为reserva
可能会返回许多潜在GET
,但您可以通过将变量声明为任何其他变量来将变量声明为global
函数和代码在GET
之前的某个地方:
var myGlobalReserva;
// Then all your other code here.
你想要实际做什么我猜它如下,使用“data-attributes”http://api.jquery.com/data/将相关数据存储在特定的“reservea”上,然后将其附加到DOM。请注意,拥有多个具有相同ID的元素无效,因此我将其更改为class="salvar"
。然后在“PUT”中,您将获得数据属性值。
$.ajax({
type: 'GET',
//async: false,
url: 'https://app.myurl.com/reservas',
success:function(reservas){
reservas.forEach (function (reserva) {
var reservasHTML = [];
reservasHTML.push('<tr class="reservas">');
reservasHTML.push('<td> <input type="checkbox" class="salvar" data-id="' + reserva._id + '" data-nome="' + reserva.nome + '" data-email="' + reserva.email + '"/> </td>');
reservasHTML.push('<td>' + reserva._id + '</td>');
reservasHTML.push('<td>' + reserva.nome + '</td>');
reservasHTML.push('<td>' + reserva.email + '</td>');
reservasHTML.push('</tr>');
$('tbody').append(reservas.join(""));
})
},
error:function(e){
console.log(e);
}
});
$(function(){
$('body').on('click', '.salvar', function(){
alert('Salvo!');
var clickedBox = $(this);
$.ajax({
type: 'PUT',
url: 'http://localhost/datas',
//async: false,
data: {
id: clickedBox.data('id'),
nome: clickedBox.data('nome'),
email: clickedBox.data('email')
},
sucess:function (success) {
console.log('success')
},
error:function(err) {
console.log(err);
}
});
});
});