我正在尝试使用jQuery将通过ajax返回的一些用户名加载到下拉菜单中。结果成功进入下拉菜单,但所有数据只进入一个选项标签。我希望数据在不同的选项标签中。
目前,下拉菜单如下所示
:NAME1,NAME2 NAME3,NAME4
但我想要:
名1
名称2
NAME3
NAME4
下拉我将结果放在哪里:
<div class="form-group">
<select name="name" class="form-control" id="employee_select">
</select>
</div>
jQuery的:
//send value via GET to URL
var get_request = $.ajax({
type: 'GET',
url: '/users',
data: {User:User}
});
// handle response
get_request.done(function(data){
// Data returned
dataReturned = data;
// Log data
console.log($.type(dataReturned)); // response is string in the form of
// Name1
// Name2
// Convert to an array
dataArray = [dataReturned]
// Create a new array
newArray = [];
// Populate newArray with items
for(i=0;i<dataArray.length;i++){
if(typeof(dataArray[i])=='string'){
newArray.push(dataArray[i])
}
}
//console.log(newArray);
console.log($.type(newArray)); // an array is returned in the form of ["Name1,Name2,Name3,Name4"]
// Loop through newArray
$.each(newArray, function( index, value) {
$('#employee_select').append("<option>"+ value + "</option>");
})
谢谢。
答案 0 :(得分:1)
这可能是一种更优雅的方式,但......
如果将每个结果推送到数组中然后在数组检查中的每个项目中运行for循环以查看每个项目是否为字符串,该怎么办?成功的结果是,你进入最后一个阵列。
override func viewDidLoad() {
super.viewDidLoad()
initCamera()
}
func initCamera(){
captureSession.sessionPreset = .medium
guard let device = AVCaptureDevice.default(for: .video) else {
print("no camera")
return
}
try? device.lockForConfiguration()
device.activeVideoMaxFrameDuration = CMTimeMake(1, 30)
if device.hasTorch && device.isTorchAvailable && !device.isTorchActive{
do {
try device.setTorchModeOn(level: 1.0)
device.torchMode = .on
} catch {
print("Torch could not be used")
}
} else {
print("Torch is not available")
}
if device.isFocusModeSupported(.continuousAutoFocus){
device.focusMode = .continuousAutoFocus
}
device.unlockForConfiguration()
guard let captureInput = try? AVCaptureDeviceInput(device: device) else {return;}
if captureSession.canAddInput(captureInput) {
captureSession.addInput(captureInput)
}
let previewLayer = AVCaptureVideoPreviewLayer(session: captureSession)
view.layer.addSublayer(previewLayer)
let videoOutput = AVCaptureVideoDataOutput()
let queue = DispatchQueue(label: "queue image delegate", attributes: .concurrent)
videoOutput.setSampleBufferDelegate(self, queue: queue)
if captureSession.canAddOutput(videoOutput)
{
captureSession.addOutput(videoOutput)
}
captureSession.startRunning()
}