1
嗯,標題可能看起來很模糊,但問題對我來說有點新。使用jQuery檢查複選框值時出錯
我有一個複選框,它使用循環(在php中)設置n次。
<input class="shiftt_class" type="checkbox" name="multiSelect[]" value="">
它是這樣設置的。
<?php foreach($depts['shifts'] as $shfts){?>
<span>
<input class="shiftt_class" type="checkbox" name="multiSelect[]" value="<?php echo $shfts['shift_id'];?>">
<select name="nottime<?php echo $shfts['shift_id']; ?>" class="notification_time_class">
<option value="">Set Time</option>
<option value="11:00" >11:00</option>
<option value="12:00" >12:00</option>
<option value="13:00">13:00</option>
</select>
</span>
<?php }?>
然後在按鈕上單擊,我tryna設置基於JSON響應選擇的複選框我使用jQuery。
我的JSON響應:
[{"shift_id":"2"},{"shift_id":"3"}]
jQuery代碼:
如果shifts
是我的JSON響應,然後
if(shifts.length>0)
{
$.each(shifts,function(index,shift)
{
if($(".shiftt_class").val()==shift.shift_id)
{
alert('ddd');
$(".shiftt_class").prop("checked", true);
}
});//end of each function
}
我得到的警報工作,但相應的複選框不組。 我得嘗試同樣的方法來設置選擇框。我哪裏做錯了?
UPDATE:我的jQuery函數
$.ajax({
url: post_url,
data:{staff_id : staff_id,csrf_test_name : csrf_token},
type: "POST",
dataType: 'json',
beforeSend: function (xhr) {
//Add your image loader here
$('#select_loader').show(); // Ajax Loader Show
},
success: function(shifts)
{
$('#select_loader').hide();
$.each(shifts, function (index, shift) {$('.shiftt_class[value="' + shift.shift_id + '"]').prop("checked", true);
});
}
});//end of ajax
它沒有被設置。 –
你有任何更有幫助的錯誤消息嗎?您可以從示例小提琴中看到,此作品給出了您的問題中的信息 –
它在小提琴中起作用。不在我的代碼中。我將發佈整個jQuery函數。看到我更新的問題。 –