在php变量中附加html如何检查空if条件

时间:2017-08-03 06:18:57

标签: javascript php jquery ajax

我需要检查php中值$的空状态,然后在下次点击提交按钮时删除旧变量。

<!DOCTYPE html>
<html>
<body>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<button type='submit' onclick='myfunction();' name='submit'>submit</button>
 <?php 
   $values ='<p id="id"></p>';
   echo $values;
      //Here I need to check empty condition or else any other suggestion is there

     ?>
 <script>
    function myfunction() {
        package_id='1212';
        packagename='airline';
        var pakage_value= package_id+","+packagename;
        $('#id').append(pakage_value);

      }
 </script>
 </body>
  </html>

3 个答案:

答案 0 :(得分:0)

在PHP中

 if(isset($values)){
    echo $values;
 }

IN js

if(typeOf(values) != 'undefined'){
    console.log(values);
}

答案 1 :(得分:0)

USE jquery

var id_content = $("#id").html();   //get this element`s content

    if(id_content.lenght){            //judge content lenght 
        $("#id").html('');            //clear 
    }else{
          myfunction();               //add
    }

答案 2 :(得分:0)

<!DOCTYPE html>
<html>
<body>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<button type='submit' onclick='click_function();' name='submit'>submit</button>
 <p id="id"></p> <script>

    function click_function(){
        var id_content = $("#id").html();  
        console.log(id_content.lenght);
        if(id_content!=''){            //judge content lenght 
            $("#id").html('');            //clear 
        }else{
              myfunction();               //add
        }
    }


    function myfunction() {
        package_id='1212';
        packagename='airline';
        var pakage_value= package_id+","+packagename;
        $('#id').append(pakage_value);

      }
 </script>
 </body>
  </html>