gpt4 book ai didi

javascript - 在两个文档 JavaScript 之间传递变量

转载 作者:行者123 更新时间:2023-11-28 05:09:14 27 4
gpt4 key购买 nike

我正在尝试创建一个由 php 数据库调用组成的下拉列表,该调用以 json 格式返回值。第一个函数 jsonload 的作用是返回包装在一个字段中的 json 的两个字段。然后我想做的是将其作为变量传递到子函数、函数的选项加载、表单中。它将作为字符串添加到其中。然后,这将为我提供该行的下拉列表,然后当我添加其他行时,下拉列表已经完成。

<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js">
</script>
<link rel="stylesheet" href="https://fonts.googleapis.com/icon?family=Material+Icons">
<link href="https://fonts.googleapis.com/css?family=Lato" rel="stylesheet">
<title>Form</title>
</head>

<body>
<form name="codexworld_frm" id="form1" method="post">
<div class="field_wrapper">
<div>
<h3><a href="javascript:void(0);" class="add_button" title="Add Field">Add POS Item</a> </h3>
</div>
</div>
<input type="submit" value="Submit">
</form>
</body>
<script type="text/javascript">
// JavaScript Document
var globalVariable;
$(document).ready(function jsonload(data){
$.getJSON("http://localhost/test/php/psql.php", function(data)
{
var options = "";
for (var i = 0; i < data.length; i++)
{
options +="<option value=\"" + data[i].POSID + "\">" + data[i].Product + "</option>";
//console.log(data[i].POSID + " " + data[i].Product);
}

var globalVariable=options;
console.log(globalVariable);
});
});


$(document).ready(function forms(){
var maxField = 50; //Input fields increment limitation
var addButton = $('.add_button'); //Add button selector
var wrapper = $('.field_wrapper'); //Input field wrapper
//var fieldHTML = {row :function(f){
//return '<h3><div>Item <input type="text" name="field_name['+f+'][]" value=""/>Material <input type="text" name="field_name['+f+'][]" value=""/><a href="javascript:void(0);" class="remove_button" title="Remove Field"><i class="material-icons" style="font-size:24px; color:blue">remove_circle</i></a></div></h3>';
//}};
var fieldHTML = {row :function optionload(f){
var local = globalVariable;
console.log(local);
//event.preventDefault();
return '<div>Item<input type="text" name="field_name['+f+'][]" value=""/>Material <select name="field_name['+f+'][]"><option value=""></option>' + local + '</select><a href="javascript:void(0);" class="remove_button" title="Remove Field"><i class="material-icons" style="font-size:24px; color:blue">remove_circle</i></a></div>';
}};
var x = 1; //initial field counter is 1
$(addButton).click(function(){ //Once add button is clicked
if(x < maxField){
x++;
$(wrapper).append(fieldHTML.row(x)); //Add field html
}
});
$(wrapper).on('click', '.remove_button', function(e){ //Once remove button is clicked
e.preventDefault();
$(this).parent('div').remove(); //remove field html
x--;
});
});
</script>

</html>

这是我的 json 文件,用于测试

[
{
"POSID": "104",
"Product": "10 Case Header"
},
{
"POSID": "105",
"Product": "10 Case Header"
}
]

我是初学者,感谢我能得到的任何帮助。

最佳答案

<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js">
</script>
<link rel="stylesheet" href="https://fonts.googleapis.com/icon?family=Material+Icons">
<link href="https://fonts.googleapis.com/css?family=Lato" rel="stylesheet">
<title>Form</title>
</head>

<body>
<form name="codexworld_frm" id="form1" method="post">
<div class="field_wrapper">
<div>
<h3><a href="javascript:void(0);" class="add_button" title="Add Field">Add POS Item</a> </h3>
</div>
</div>
<input type="submit" value="Submit">
</form>
</body>
<script type="text/javascript">
// JavaScript Document
var globalVariable;
$(document).ready(function jsonload(data){
// $.getJSON("http://localhost/test/php/psql.php", function(data)
//{
var options = "";
var data =[
{
"POSID": "104",
"Product": "10 Case Header"
},
{
"POSID": "105",
"Product": "10 Case Header"
}
];
for (var i = 0; i < data.length; i++)
{
options +="<option value=\"" + data[i].POSID + "\">" + data[i].Product + "</option>";
//console.log(data[i].POSID + " " + data[i].Product);
}

var globalVariable=options;
console.log(globalVariable);
//});

var maxField = 50; //Input fields increment limitation
var addButton = $('.add_button'); //Add button selector
var wrapper = $('.field_wrapper'); //Input field wrapper
//var fieldHTML = {row :function(f){
//return '<h3><div>Item <input type="text" name="field_name['+f+'][]" value=""/>Material <input type="text" name="field_name['+f+'][]" value=""/><a href="javascript:void(0);" class="remove_button" title="Remove Field"><i class="material-icons" style="font-size:24px; color:blue">remove_circle</i></a></div></h3>';
//}};
var fieldHTML = {row :function optionload(f){
var local = globalVariable;
console.log(local);
//event.preventDefault();
return '<div>Item<input type="text" name="field_name['+f+'][]" value=""/>Material <select name="field_name['+f+'][]"><option value=""></option>' + local + '</select><a href="javascript:void(0);" class="remove_button" title="Remove Field"><i class="material-icons" style="font-size:24px; color:blue">remove_circle</i></a></div>';
}};
var x = 1; //initial field counter is 1
$(addButton).click(function(){ //Once add button is clicked
if(x < maxField){
x++;
$(wrapper).append(fieldHTML.row(x)); //Add field html
}
});
$(wrapper).on('click', '.remove_button', function(e){ //Once remove button is clicked
e.preventDefault();
$(this).parent('div').remove(); //remove field html
x--;
});
});
</script>

</html>

关于javascript - 在两个文档 JavaScript 之间传递变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41511968/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com