将值推入数组jQuery [英] Push values into array jquery
问题描述
我在表格的开头创建了一个空数组
I have created an empty array at the beginning of the form
<script>
var monthly_values = [];
var i=0;
</script>
在循环中尝试实现以下代码.... 但是如果没有警报"框,或者如果我删除"ready(function())",则推送将不起作用
Inside the loop trying to achieve below code.... but the push doesn't work without "alert" box nor if i remove "ready(function())"
$prodline=500$ /// CGI script loops till 500 iterations
<script>
$(document).ready(function(){
i++;
monthly_values.push("$premium$");
setTimeout(function() {
alert("$premium$");
}, 1);
});
</script>
$/prodline$//end of the iteration...
我将值从循环推入可以工作的数组中.但是我需要删除警报框,或者至少从警报框中单击确定".
I push values from loop into array which is working .But i need to remove alert box or at least OK from alert box ..
整个代码在这里.
<html>
<head>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.js"></script>
<title>PHP FILE - $licensee$</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<meta name="Author" content="COMPULIFE Software, Inc.">
<link href="../../css/styles_website.css" type="text/css" rel="stylesheet">
<link href="../../css/termsampler.css" type="text/css" rel="stylesheet">
<link href="../../css/$css$" type="text/css" rel="stylesheet">
<script>
var monthly_values = [];
var i=0;
</script>
<script language="JavaScript" type="text/javascript">
// var monthly_values = [];
// var i=0;
function getbest (ambest, company, compcode)
{
document.comparisonform.AmBest.value = ambest;
document.comparisonform.CompanyName.value = company;
document.comparisonform.CompanyCode.value = compcode;
document.comparisonform.OnClick=abc=window.open('','reason','width=700,height=500,left=20,top=20,scrollbars=yes');
document.comparisonform.target='reason';
abc.focus();
document.comparisonform.submit();
}
</script>
</head>
<body>
</tr>
<style>
.loader {
border: 16px solid #f3f3f3;
border-radius: 50%;
border-top: 16px solid #3498db;
width: 120px;
height: 120px;
-webkit-animation: spin 2s linear infinite;
animation: spin 2s linear infinite;
margin: auto;
padding: 10px;
}
@-webkit-keyframes spin {
0% { -webkit-transform: rotate(0deg); }
100% { -webkit-transform: rotate(360deg); }
}
@keyframes spin {
0% { transform: rotate(0deg); }
100% { transform: rotate(360deg); }
}
</style>
</head>
<body>
<div class="loader" style ></div>
$prodline=500$
<script>
$(document).ready(function(){
i++;
monthly_values.push("$premium$");
setTimeout(function() {
alert("$premium$"+"$company$"+"$product$"+"$healthcat$"+"$PremiumAnnual$"+"$Premium$"+"$rgpfpp$"+"$guar$"+"$policyfee$"+"$comp$"+"$prod$"+"$rowEvenOdd$");
}, 1);
});
</script>
$/prodline$
</table>
</form>
<br><br>
<script>
$(document).ready(function(){
var len=monthly_values.length;
var min_quote=monthly_values [0];
var max_quote=monthly_values [monthly_values.length-1];
window.location = 'http://loalhost/laravel/public/customised-quote?min_quote_val='+min_quote+'&max_quote_val='+max_quote;
});
</script>
推荐答案
此代码应该可以正常工作,请查看下面的代码段. 您是否在HTML中包含jQuery?
This code should work without problems, check the snippet below. Are you including jQuery in your html?
var monthly_values = [];
var i=0;
$(document).ready(function(){
i++;
monthly_values.push("$premium$");
setTimeout(function() {
console.log(monthly_values)
}, 1);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
编辑,看到我更新的代码后,如果我理解正确的话,则在您的循环中,您将文档$(document).ready()片段渲染了500次,这是完全错误的方法.
EDIT after seeing your updated code, if i understand it right, in your loop you are rendering the the document $(document).ready() snippet 500 times, which is completely wrong approach.
不确定您使用的是哪种语言,以及您到底想用循环实现什么,但是您可以直接将$ prodline $数组直接打印到javascript数组变量中,而不必循环遍历
Not sure what language you are using, and what exactly are you trying to achieve with your loop, but you could just print your $prodline$ array directly into javascript array variable without having to loop through it
此外,现在我在文件末尾的$(document).ready()中注意到,您正在立即重定向到新的url,如果这是您想要的结果,请在进行重定向之前生成数组,如下所示:
Also, now i noticed in your $(document).ready() at the end of your file you are redirecting to new url immediately, if this is your intended result, generate the array just before you redirect like this:
<script>
$(document).ready(function(){
var monthly_values = []
$prodline=500$
monthly_values.push("$premium$")
$/prodline$
var len=monthly_values.length;
var min_quote=monthly_values [0];
var max_quote=monthly_values [monthly_values.length-1];
window.location = 'http://localhost/laravel/public/customised-quote?min_quote_val='+min_quote+'&max_quote_val='+max_quote;
})
</script>
这篇关于将值推入数组jQuery的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!