phd12tho 2015-01-08 16:53 采纳率: 100%
浏览 4247
已采纳

nvelocity中的#if()如何在JavaScript中使用?

nvelocity中的if跟JavaScript中的if关键字冲突了,怎么想办法可以用nvelocity中的#if

 <script type="text/javascript">
     function GetTestType() {
         var s = document.getElementById("testType");
         $selectedType =s.options[s.selectedIndex].text;
         alert($selectedType);
         var n = document.getElementById("testName");

        #foreach($row in $Data.testData)
             #if($row.TestType==$selectedType)
                n.options.add(new Option("text", "value"));
               #end
         #end
     }
</script>

这个是按照你的改的:

 </head>
<script src="../javascript/jquery-1.7.2.js" type="text/javascript"></script>
 <script type="text/javascript">

     var testData = $Data.testData;
     function GetTestType() {

         var s = document.getElementById("testType");
         var n = document.getElementById("testName");
         var selectedType = s.options[s.selectedIndex].text;

         var i;
         for (i = n.length-1; i >=0; --i) {
             n.remove(i);  
         }
         alert(testData.length); //这个没反应。。。说明testData没有被赋值。
         for (i = 0; i < testData.length; ++i) {
             var row = testData[i];

             if (row.TestType == selectedType) {
                 n.options.add(new Option(row.TestName, row.Id));
             }
         }
  }
</script>

<script type="text/javascript">
    function GetSelectedId() {
        var s = document.getElementById("testName");
        $selectedId = s.options[s.selectedIndex].id
        alert($selectedId);

    }
</script>
<body>

图片说明

  • 写回答

4条回答

  • save4me 2015-01-09 16:11
    关注

    既然你是用了js,我认为你的testName这个下拉框的数据$Data.testData是一次性都传给客户端的,如果是这样的话,foreach, if等,都是纯js代码,不需要使用nvelocity的语句,需要的是把$Data.testData赋值给一个js变量,比如var testData = $Data.testData;
    我写了一个简单的演示代码,你可以点击查看

     //var testData = $Data.testData;
    var testData = [
        {TestType: 'Type 1', Text: 'Text 1', Value: 'Value 1'},
        {TestType: 'Type 2', Text: 'Text 2', Value: 'Value 2'},
        {TestType: 'Type 3', Text: 'Text 3', Value: 'Value 3'},
        {TestType: 'Type 1', Text: 'Text 4', Value: 'Value 4'},
        {TestType: 'Type 2', Text: 'Text 5', Value: 'Value 5'},
        {TestType: 'Type 3', Text: 'Text 6', Value: 'Value 6'},
        {TestType: 'Type 1', Text: 'Text 7', Value: 'Value 7'},
        {TestType: 'Type 2', Text: 'Text 8', Value: 'Value 8'}
    ];
    function GetTestType(){
        var s = document.getElementById("testType");
        var selectedType = s.options[s.selectedIndex].text;
        //alert(selectedType);
        var n = document.getElementById("testName");
    
        var i;
        for (i = n.length - 1; i >= 0; --i) {
            n.remove(i);
        }
        //forEach(row in testData){
        for(i = 0, l = testData.length; i < l; ++i){
            var row = testData[i];
            if(row.TestType == selectedType){
                n.options.add(new Option(row.Text, row.Value));
            }
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能
  • ¥15 jmeter脚本回放有的是对的有的是错的
  • ¥15 r语言蛋白组学相关问题