如何在< script>中执行foreach循环标签 ?请注意它不是< script type =" text / javascript"> [英] How do I perform foreach loop inside <script> tag ? note its not <script type="text/javascript">

查看:82
本文介绍了如何在< script>中执行foreach循环标签 ?请注意它不是< script type =" text / javascript">的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  var  a = []; 
var b = [];
@foreach( var filename in Model.File)
{
@:a.push(' @ filename');
}
@foreach( var errortext in Model.Error)
{
@:b.push(' @ errortext');
}







上面给了我:

< pre lang =text> a [0] =File1;
a [1] =File2;
b [0] =无法打开报告附件[iRefpdfFile.PDF]。这不会对以任何方式在线提交此文件产生不利影响。错误:无法处理iref流。此PDF需要重新保存Acrobat 5兼容的方式或原始保存选项已更改,以便它不使用此功能。
b [1] =这是一条演示错误消息。





保持文件名和错误的顺序,即b [0]是[0]的错误消息。

但是我想显示每个文件的警报及其连续的错误消息。我怎么做?



我尝试过的事情:



我尝试执行@foreach循环,但这不允许我使用alert关键字或任何javascript方法。

请注意代码在< script>内部标签而不是里面< script type =text / javascript>

解决方案

inArray [ ^ ]方法需要一个数组作为其第二个参数。您正在传递字符串。



要查找字符串中子字符串的索引,请使用 indexOf [ ^ ]:

  var  texttosearchfrom = a [ 0 ]; 
var found = texttosearchfrom.indexOf(' Acrobat');



如果您想要不区分大小写,请使用 search [ ^ ],不区分大小写正则表达式 [ ^ ]:

  var  texttosearchfrom = a [ 0 ]; 
var found = texttosearchfrom.search(/ Acrobat / i);


var a = [];
var b=[];
@foreach (var filename in Model.File)
{
    @:a.push('@filename');
}
@foreach (var errortext in Model.Error)
{
    @:b.push('@errortext');
}




The above gives me :

a[0]="File1";
a[1]="File2";
b[0]= "Unable to open report attachment [iRefpdfFile.PDF]. This will not adversely affect the submission of this file online in any way. Error: Cannot handle iref streams. This PDF will need to be resaved in an Acrobat 5 compatable manner or with the original save options changed so that it does not to use this feature."
b[1]="This is a demo error message."



The sequence of filename and error is maintained that is b[0] is a[0]'s error message.
But i want to display alert for every file and its consequtive error message. How do i do that ?

What I have tried:

I tried to perform @foreach loop but that doesnt allow me to use alert keyword or any javascript method .
Note the code is inside <script> tag and not inside <script type="text/javascript">

解决方案

The inArray[^] method expects an array as its second parameter. You are passing a string.

To find the index of a substring within the string, use indexOf[^]:

var texttosearchfrom = a[0];
var found = texttosearchfrom.indexOf('Acrobat');


If you want a case-insensitive match, then use search[^] with a case-insensitive Regular Expression[^] :

var texttosearchfrom = a[0];
var found = texttosearchfrom.search(/Acrobat/i);


这篇关于如何在&lt; script&gt;中执行foreach循环标签 ?请注意它不是&lt; script type =&quot; text / javascript&quot;&gt;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆