문제

function myfunc()
{

   var x = document.getElementById('MY_DIV').name;
   document.getElementById(x).value=2;
}


      <input type="text" id="MY_DIV" name="MY_DIV1"/>
      <input type="text" id="my_div1" />
      <input type="button" name="submit" value="submit" onclick="myfunc();">

How to use ignorecase in above js code to fill the value 2 in second textbox?

도움이 되었습니까?

해결책 2

Javascript string case-insensitive comparisons can be performed with string.toUpperCase.

var x = document.getElementById('MY_DIV').name;
x = x.toUpperCase(); //use this line
    if (x == "STRING TO MATCH"){
    }

Referenced to tutorialsPoint Example for using ignoreCase property

<html>
<head>
<title>JavaScript RegExp ignoreCase Property</title>
</head>
<body>
<script type="text/javascript">
   var re = new RegExp( "string" );

   if ( re.ignoreCase ){
      document.write("Test1-ignoreCase property is set"); 
   }else{
     document.write("Test1-ignoreCase property is not set"); 
   }
   re = new RegExp( "string", "i" );

   if ( re.ignoreCase ){
      document.write("<br/>Test2-ignoreCase property is set"); 
   }else{
     document.write("<br/>Test2-ignoreCase property is not set"); 
   }
</script>
</body>
</html>

Output

Test1 - ignoreCase property is not set

Test2 - ignoreCase property is set

Code updated for D.K function

function myfunc()
{

    var x = document.getElementById('MY_DIV').name;
    //x = x.toUpperCase();   // check the result with / without un-commenting this line
    
    var re = new RegExp( x );

    if ( re.ignoreCase ){
      document.write("X - Test1-ignoreCase property is set"); 
    }else{
     document.write("X - Test1-ignoreCase property is not set"); 
    }
    re = new RegExp( x, "i" );

    if ( re.ignoreCase ){
      document.write("<br/> X - Test2-ignoreCase property is set"); 
    }else{
     document.write("<br/>X - Test2-ignoreCase property is not set"); 
    }
    x = x.toUpperCase();  // ignoring case
    document.getElementById(x).value=2;
   
}

다른 팁

Use this in js:

   var x = document.getElementById('MY_DIV').name;
   x = x.toLowerCase(); //use this line
   document.getElementById(x).value=2;

JavaScript is case sensitive you can use the following but it will not work if your target div id has any upper case characters

var x = document.getElementById('MY_DIV').name.toLowerCase();
document.getElementById(x).value=2;
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top