2014-11-09 74 views
0

我试图把条件与jQuery提交按钮,似乎无法使任何工作。如果medLength == 0那么应该有一个错误消息。没有错误信息,即使medLength有一个值,提交按钮不起作用。提交按钮有一个图形,所以我不能将其更改为按钮。任何帮助,将不胜感激。JQuery提交按钮的条件

$("#submit").click(function get() { 
    if (medLength == 0) { 
     $(".errorStop").css().show; 
    } else { 

     $.ajax({ 
      url: "postnew1.php", 
      type: "POST", 
      data: { 
       type: medType, 
       time: medLength, 
       length: counter 
      }, 
     }).success(function (response) { 
      location.href = "meditate.php"; 
     }).error(function (e) {}); 
    } 
}); 

下面的代码并没有if/else声明工作:

$("#submit").click(function get() { 
    $.ajax({ 
     url: "postnew1.php", 
     type: "POST", 
     data: { 
      type: medType, 
      time: medLength, 
      length: counter 
     }, 
    }).success(function (response) { 
     location.href = "meditate.php"; 
    }).error(function (e) {}); 
}); 
+2

什么是medLength? – 2014-11-09 04:16:53

+0

我认为你应该这样做:if(medLength。长度== 0){ – 2014-11-09 04:18:58

+0

您在使用它之前是否曾想过检查'medLength'包含的内容? – 2014-11-09 04:58:35

回答

1

一种方法来调试这将是添加alert("Error")到若情况下,这样你就可以验证,如果这些代码确实被触发,像这样:

if (medLength ==0){ 
    alert("Error"); 
    $(".errorStop").css().show;   
} 

但是我能够看到你的错误信息可能没有显示的部分原因是你没有调用show()函数。

变化

$(".errorStop").css().show; 

$(".errorStop").show(); 
1

我觉得应该是

$(".errorStop").css().show; 

$(".errorStop").show(); 

希望你获得medLength的权利!

+0

应该是$(“。errorStop”)。show(); – 2014-11-09 04:24:31

+0

更正!谢谢 – 2014-11-09 04:27:47

0

“提交按钮有一个图形,所以我不能将其更改为按钮。”你不需要“按钮”是一个type =“button”或type =“submit”。说你可以为元素设置一个id,比如id =“btn_sendInfo”。我不明白medLength的价值来自哪里。所以这段代码应该可以工作:

NOte =类型。我设置了json,但是如果你不想让你可以随意改变,或者删除并让jquery“gess”处理。 如果你不想用jason发送数据fron服务器,你可以擦除// JSON循环来检索信息。

//Your javaScript code 
$(document).on("click", "#btn_sendInfo", function(){ 

if(medLength === 0){ 
    $(".errorStop").show() 
} 
else{ 

    //Data you want to send to php evaluate 
    var dt={ 
       ObjEvn:"btn_sendInfo", //You can use $ObjEvn=$_POST[ObjEvn] on server side php and test if($ObjEvn="btn_sendInfo"){do something ....} 
       type: medType, 
       time: medLength, 
       length: counter, 
       OtherDataJsToPHP: $("#txt_EmailLogin").val() 

      }; 

    //Ajax  
    var request =$.ajax({//http://api.jquery.com/jQuery.ajax/ 
          url: "postnew1.php", 
          type: "POST", 
          data: dt, 
          dataType: "json ????????" 
         }); 

    //Ajax Done catch JSON from PHP 
     request.done(function(dataset){ 

      // Put your succes function here ===> location.href = "meditate.php"; 

      //JSON Or if want to use json you cand use this to retrieve information send from server through json 
      for (var index in dataset){ 
       dataPHPtoJsJS=dataset[index].dataPHPtoJs; 
       asManyasYouWantJS=dataset[index].asYouWant; 
      } 


      //JavaScript conditions. Here you can control the behaivior of your html object, based on your PHP response 
      if(dataPHPtoJsJS){ 
       $("#idYourHtmlElement").removeClass("class1") 
       $("#idYourHtmlElement").addClass("class2") 
      } 


    }); 

    //Ajax Fail 
     request.fail(function(jqXHR, textStatus) { 
      //Put your function ---- function (e) {} 
      alert("Request failed: " + textStatus); 
     }); 
    } 

} 

但是,如果你只想要解决您的问题做到这一点,尽管你Seconde系列代码:

$(document).on("click", "#sendInfo", function(){ 

if(medLength === 0){ 
    $(".errorStop").show() 
} 
else{ 
     //your ajax code 
    } 

} 
0

如通过下面的演示,图像按钮的工作原理就像一个提交按钮,不提交父母形式。其次,演示还显示提交处理程序中的代码是有条件的;代码的不同部分在不同的点击次数下执行,具体取决于变量的值。

因此,这个演示应该是一个很好的例子,可以帮助您完成原本的目标。

var medLength = 0; 
 
    $('form').on('submit', function(e) { 
 
    e.preventDefault(); 
 
    if(medLength === 0) { 
 
     alert('medLength = 0'); 
 
    } else { 
 
     alert('medLength != 0'); 
 
    } 
 
    medLength++; 
 
    });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<form> 
 
    First name: 
 
    <input type="text" name="fname"> 
 
    <br> 
 
    <input type="image" src="https://encrypted-tbn1.gstatic.com/images?q=tbn:ANd9GcQnMxVaix7dQzuVgQa5rwGcrX6sdy5vlvel5djnQgcn926d1FgH" alt="Submit"> 
 
</form>