JS / Ajax警报框错误

时间:2014-12-18 14:00:56

标签: javascript html ajax

我有一个警告框,即使$imagename为空,也会提示"图片已上传"

这是脚本:

<script>
  function ajax_post1(ca){

    var cat = ca;
    var name = document.getElementById("name").value; 
    var desc = document.getElementById("description").value;
    var key = document.getElementById("keyword").value;
    var image = document.getElementById("image").value; 

    if ($.isEmptyObject(image)) {
      alert('pls upload your image')
    } else {
      alert(' image uploaded ')
    }

    var myData = 'content_ca='+ cat + '&content_desc='+desc+ '&content_key='+key+ '&content_name='+name;//build a post data structure

    jQuery.ajax({
      type: "POST", // HTTP method POST or GET
      url: "uploadsignuppackageresponse.php", //Where to make Ajax calls
      dataType:"text", // Data type, HTML, json etc.
      data:myData, //Form variables
      success:function(response){

      //$("#imagebox").append(response);
      //$("#contentText").val(''); //empty text field on successful
      //alert("haha");

      }, error:function (xhr, ajaxOptions, thrownError){
        alert(thrownError);
      }
    });

  };
</script>

这是主页:

<?php
  $sql1 = mysql_query ("SELECT * FROM dumimage WHERE email = '$user_signup' AND cat='company' ");
  $row = mysql_fetch_array($sql1);
  $imagename = $row['name'];
?>

Name: 
  <input id="name" type="text"  ></input>
  <input id="image" type="hidden" value="<?php echo $imagename ?> "></input>
Description
  <textarea id="description" rows="7" cols="42"></textarea>
Keywords: 
  <input id="keyword" type="text" placeholder="3 Maximum Keywords" ></input>

<input type="submit"  value="Upload" class="pre" style="float:left; onClick="ajax_post1('company')">   

2 个答案:

答案 0 :(得分:0)

尝试此操作以查看对象是否为空

if (image.length < 1) {
      alert('pls upload your image')
} else {
      alert(' image uploaded ')
}

答案 1 :(得分:0)

尝试替换此行:

if ($.isEmptyObject(image)) {

有了这个:

if (image != '') {

您还必须更正您的PHP代码,因为您已将括号关闭在错误的位置并且您缺少分号:

<input id="image" type="hidden" value="<?php echo $imagename;?>"></input>