【发布时间】:2019-10-21 09:49:31
【问题描述】:
所以基本上我正在研究 javascript 和 jquery,因为看起来我可以在我的项目中使用它。
今天我试图深入了解一下:我有一个数据库,其中包含文章 ID、Ean 代码、品牌和参考资料。 我想做的是:您在一个输入中键入文章编号,然后它会填充其他 3 个。
我环顾了一圈,然后科学地编写了我自己的第一个代码。它在大多数情况下都有效。 我想不通的是如何用我从数据库获得的数组中的数据而不是我自己设置的数组填充 const 值。 我尝试了几件事,但是是的,我缺乏基础知识,所以我想这对你们来说是微不足道的
提前感谢您的帮助!
<?php
$sql = "SELECT * FROM prescreen_articles";
$result = mysqli_query($conn, $sql);
while ($articles = mysqli_fetch_array($result)){
$temp_articles[] = array(
"narticle" => $articles['article_number'],
"ean" => $articles['EAN'],
"brand" => $articles['brand'],
"ref" => $articles['ref']
);
}
?>
<input name="artible" id="article" type="text">
<input name="ean" id="ean" type="text" disabled>
<input name="brand" id="brand" type="text" disabled>
<input name="ref" id="ref" type="text" disabled>
<script src="https://code.jquery.com/jquery-3.2.1.min.js"></script>
<script>
jQuery(document).ready(function() {
const $main = $('#article');
const $ean = $('#ean');
const $brand = $('#brand');
const $ref = $('#ref');
var id;
var array_articles = <?php echo json_encode($temp_article); ?>;
const values = {
article: ['2222222', '9999999', '1111111'],
ean: ['1', '2', '3'],
brand: ['samsung', 'apple', 'huawei'],
ref: ['S10', 'xr', 'P30'],
};
$main.on('keyup', function () {
if($main.is(':empty') || $main.lenght < 7){
ean.value = '';
brand.value = '';
ref.value = '';
}
if (jQuery.inArray(this.value , values.article) !== -1) {
id = jQuery.inArray(this.value ,values.article);
ean.value = values.ean[id];
brand.value = values.brand[id];
ref.value = values.ref[id];
}
});
});
</script>
【问题讨论】:
-
确实如此:所以我希望 array_articles 用相关数据填充值 article、Ean、brand、ref
-
那么您为什么不首先将
$array_articles构造成您想要的格式?
标签: javascript php jquery arrays