从html data-enum元素传递整数值

时间:2015-10-30 11:02:47

标签: javascript jquery

package classes;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import org.joda.time.DateTime;

public class MyListener implements ServletContextListener {

    @Override
    public void contextInitialized(ServletContextEvent sce) {
        System.out.println("Project deployed: " + new DateTime());
        //or any other form of output
    }

    @Override
    public void contextDestroyed(ServletContextEvent sce) {
        System.out.println("Project undeployed: " + new DateTime());
        //or any other form of output
    }
}

我将'e'作为字符串EnumTypeA而不是1.我应该如何传递它以将其作为整数?

1 个答案:

答案 0 :(得分:3)

因为data-enum属性值是"字符串",不是对象或对象变量或整数或...

执行此迁移可以帮助您:

<p data-enum=EnumTypeA> </p>    
var NewEnum= { EnumTypeA: 1 }    
var e = $(this).data('enum');
var eint = parseInt(NewEnum[e]);

修改

以下是一个显示其工作原理的摘录:

&#13;
&#13;
var NewEnum= { EnumTypeA: 1 }

function getInt(p) {
  
  var e = p.getAttribute('data-enum');
  var eint = parseInt(NewEnum[e]);
  
  var chkvalue = document.getElementById("chkvalue");
  chkvalue.innerHTML = "The enum values is: " + eint;
  
}
&#13;
<p onclick="getInt(this);" data-enum=EnumTypeA> Click here </p>
<p id="chkvalue"> </p>
&#13;
&#13;
&#13;