简体   繁体   English

如何在带有if-else语句的javascript中使用其ID调用表

[英]How to call a table using its Id in a javascript with if-else statements

Am trying to show a table using a value "graph". 我正在尝试使用值“ graph”显示表格。 if graph value is 0 then it should display table1(id of table), if value of graph is 10 , it should display table2(id of table). 如果图的值为0,则应显示table1(表的ID);如果图的值为10,则应显示table2(表的ID)。 am trying to show this with java script with if else case. 我正在尝试使用Java脚本在其他情况下显示此内容。 i don't know how to call a table within java script in if else statements 我不知道如何在if语句中的java脚本中调用表

               <script>
    $(document).ready( function () {

        myFunc();

    });
    function myFunc(){

        console.log("input value", document.getElementById("graph").value)  
        if (document.getElementById('graph').value==00){


        }


    }</script>

how to write here to call a table in if else statement 如何在此处编写以在if else语句中调用表

Assuming both tables are available on the document when the function is called, it should look something like this.... 假设调用该函数时两个表在文档上均可用,则其外观应如下所示。

<script>
    $(document).ready( function () {
    myFunc();
});

function myFunc(){
    console.log("input value", document.getElementById("graph").value);
    var val = document.getElementById("graph").value;
    document.getElementById('table1').style.display = val== 0 ? '' : 'none';
    document.getElementById('table2').style.display = val== 10 ? '' : 'none';
}</script>

if you want to do it in a if else if.... 如果您想在其他情况下进行...

    //First hide both tables
    document.getElementById('table1').style.display = 'none';
    document.getElementById('table2').style.display = 'none';
    var val = document.getElementById("graph").value;

    //then show the one we want
    if(val==0)
        document.getElementById('table1').style.display = '';
    else if(val == 10)
        document.getElementById('table2').style.display = '';

Hope that helps 希望能有所帮助

Try doing with Jquery if possible . 如果可能,尝试使用Jquery Hope it helps ! 希望能帮助到你 !

$(document).ready(function(){
   if($('#graph').val()=='00'){
        $('#table1').show();
        $('#table2').hide();
     }else if($('#graph').val()=='10'){
            $('#table2').show();
            $('#table1').hide();
             }
});

Try this one...it has been tested... 试试这个...它已经过测试...

    jQuery(document).ready( function () {
       myFunc();
    });
   function myFunc()
   {

            console.log("input value", document.getElementById("test").value);  
             if (document.getElementById('test').value=="testing"){

                alert("got in if");
             }

            else 
           {
            //this will hit the column of a table now change it according to your requirements
           //you may write $ instead of jQuery,I assigned a value to a column you set it as you need...
          jQuery("#your_target_id_in_table").val("hello"); 
          alert(jQuery("#your_target_id_in_table").val()); //you may write $ instead of jQuery
          }


  }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM