javascript in jsf/icefaces

前端 未结 3 665
渐次进展
渐次进展 2020-12-11 09:29

I have file with jspx extension i write javascript like

function isNumber(inputId){

            var value = document.getElementById(\'mainForm:\'+ inputId)         


        
相关标签:
3条回答
  • 2020-12-11 09:46

    Enclose your Javascript in CDATA Sections:

    <script language="javascript" type="text/javascript">
    /* <![CDATA[ */
    
        function isNumber(inputId){
    
                var value = document.getElementById('mainForm:'+ inputId).value;
                var  s = value.length;
    
                while(s >= 0){
                    var c = value.charAt(s);
                    if(c > "9"){
                        alert("Value must be digit");
                        document.getElementById('mainForm:'+ inputId).value = "";
                        document.getElementById('mainForm:'+ inputId).focus();
                        return false;
                    }
                    s --;
                }
                return true;
            }
    
            //Code containing "<" also comes in this section
    
    /* ]]> */
    </script>
    
    0 讨论(0)
  • 2020-12-11 09:47

    Besides the escaping and CDATA answers:

    If you want to check if a value is a number, there is a javascript built-in function for that: isNaN

    Here is an example:

    if (isNaN(document.getElementById('mainForm:'+ inputId).value))
      {
        alert("Please enter digits");
        document.getElementById(obj).focus();
        document.getElementById(obj).select();
        return false;
      }
    
    0 讨论(0)
  • 2020-12-11 09:58

    As explained by Matt Handy, you could not use the < or > sign in your JSPX, as this is a XML format. You have three solutions regarding your problem:

    • Escape by using &lt; or &gt;.
    • Use <![CDATA[ ... ]]> to hold your JavaScript code in your page.
    • Set your JavaScript code in a separate .js file, and load it in your JSPX page.
    0 讨论(0)
提交回复
热议问题