【发布时间】:2021-12-13 10:00:07
【问题描述】:
您能帮我如何在输入中拆分字符串并将这些拆分字符串插入到不同的输入中吗?我已经尝试了一些在此处找到的代码,但仍然无法正常工作。
我实际上想在执行函数后将字符串存储在这 2 个不同的输入(未隐藏的输入)中
这是源代码:
function split() {
var strings = $("#qrstring").attr("value").split("<br>");
if (strings.length == 2) {
qrData(strings, 1);
}
}
function qrData(value, index) {
if (value.length == 2) {
$(".idnum").attr("value", value[0]);
$(".idname").attr("value", value[1]);
} else {
alert("malformed strings");
}
}
let scanner = new Instascan.Scanner({
video: document.getElementById('preview'),
mirror: false
});
scanner.addListener('scan', function(c) {
window.document.getElementById('qrstring').value = c;
});
Instascan.Camera.getCameras().then(function(cameras) {
if (cameras.length > 0) {
scanner.start(cameras[0]);
} else {
console.error('No cameras found.');
alert('No cameras found.');
}
}).catch(function(e) {
console.error(e);
alert(e);
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
<input type="hidden" id="qrstring" value="" split(); qrData();>
<label id="label">Account ID </label>
<input type="text" name="accId" required="" class="idnum" style="width: 108px;margin-right: 15px;" value="">
<br/>
<label id="label">Customer Name </label>
<br/>
<input type="text" readonly="" name="name" class="idname" required="" value="">
【问题讨论】:
-
这似乎是 JavaScript 代码,而不是 PHP。我已经为你重新标记了它。如果你没有使用正确的标签,你的问题将不会被有正确技能的人看到来帮助你!
-
value="" split(); qrData();看起来像是一厢情愿。 -
你想什么时候拆分?隐藏字段是否已经有值?如果是这样,为什么不在服务器上填写字段?
-
@ADyson 哦对不起呵呵
-
@mplungjan 这是隐藏的,因为我想存储扫描的字符串数据,因为我假设只有当字符串在输入字段中时才能拆分字符串。呵呵,如果我错了,请告诉我
标签: javascript html jquery input split