Jquery 循环一次或在数据数组中显示一次数据
Jquery loop once or show data once in array of data
我正在使用$.get()请求从Laravel控制器获取数据并在Javascript中获取数据数组。
function getSpots1(){
var spots = $('#event-spots1');
spots.empty();
spots.html('<p><i class="uk-icon-refresh uk-icon-medium uk-icon-spin"></i></p>');
var event_id = $("#event-event_id").val();
var event_date = $("#event-date").val();
var code = '';
console.log(event_date);
$.get("/1/users/spots/" + event_id + "/" + event_date + "/date", function(data) {
spots.empty();
console.log(data);
code += '<label for="event_time" class="uk-form-label">Select Time</label><select class="uk-width-1-1" name="event_time" id="event-time">';
$.each(data.spots, function(index, value)
{
code += '<option value="' + value.event_time + '">' + value.event_time + '</option>';
});
code += '</select><p class="uk-form-help-block uk-text-muted">The spot you''re booking</p>';
code += '</br><div class="uk-margin-bottom"><label for="event_time" class="uk-form-label">Price</label>';
$.each(data.spots, function(index, value)
{
code += '<input type="text" class="uk-width-1-1" value="' + value.price + '" disabled />';
});
spots.append(code);
getSpots2();
});
}
我有 $.each() jquery 方法来循环数据数组,但对于第二个 $.each() 方法,我只需要循环一次。我得到值.价格多次,具体取决于循环长度。对于第一个 $.each,我需要一个循环,因为我有选择输入字段。有没有另一种方法代替 $.each 循环一次数据变量?我是新手,以前没有真正编写过Jquery或Javascript。
好吧,
你可以像data.spots[0].event_time
这样获取data.spots
中的第一个对象,然后你根本不需要循环。
不太推荐,您可以在循环的末尾放置一个return false;
,例如:
$.each(data.spots, function(index, value)
{
code += '<input type="text" class="uk-width-1-1" value="' + value.price + '" disabled />';
return false;
});
这会立即停止循环。
相关文章:
- 如何在chrome扩展中存储数据/结果,以及如何使用setTimeout使其只被调用一次
- 数据互绑定问题:转换器只运行一次,无法绑定元素的 ID
- Jquery 循环一次或在数据数组中显示一次数据
- 如果发现任何数据,如何停止每7秒运行一次的函数
- Ajax 使用函数每秒获取一次 php 数据
- jqueryhttp将数据发布到整个数组,一次发送一个
- 将数据加载到本地存储一次
- 在用户为下一次输入插入数据后清空文本字段
- 如何在用户滚动到页面底部时仅加载一次数据
- 一次打开一行详细信息数据表
- 如何使用上一次调用的数据进行第二次 JS .post 调用
- Socket.io - 每 34 毫秒接收一次数据
- Canvas getImageData() 为了获得最佳性能.提取所有数据或一次提取一个数据
- 数据表每 30 秒自动刷新一次不起作用
- 如何在索引数据库中仅添加一次初始数据
- 在滚动时没有获取新数据,而是在滚动时一次又一次地添加相同的 json 数据
- 如何每 n 分钟使用数据更新一次 Web 应用程序
- 如何一次获取一页的数据
- 我如何让我的angularjs工厂只获取http数据一次
- AngularJS应用程序:从JSON加载数据一次,然后在多个控制器中使用它