2012-05-28 24 views
0

我想輸入值提交到輸入字段,所以當我從輸入字段clcking出方將輸入字段我將運行Ajax代碼和發送值輸入dataString到相同的文件,在這種情況下cald ajax.php。 感謝我如何從輸入字段發佈數據,而不通過AJAX

我的代碼:

$(document).ready(function() 
{ 



    var dataString; 

$("valueforajax").mouseleave(function() {//when we leave the field. 
    alert('test it is mouseup '); 
    dataString=$("#valueforajax").val(); 
    alert(dataString); 
    if ("" = dataString){//it is mean that input field not empty 
     $.ajax({ 
     type: "POST", 
     url: "../../ajax.php", 
      data: dataString, 
      cache: false, 
      success: function(html) 
      { 
      alert("There is submited sucsses"); 
      } 
      });//ajax 
     }//if 
    });//mouseup 
});//ready 

ajax.php

<?php 
    if (isset($_POST['dataString'])){ 
     echo ("dataString not empty:= ".$_POST['dataString']);} 
    ?> 
    <html> 
    <head> 
    <link rel="stylesheet" type="text/css" href="./public/stylesheets/stylesheets.css" > 
    <script type="text/javascript" src="./public/js/jquery-1.7.1.min.js"></script> 
    <script type="text/javascript" src="./public/js/ajax.js"></script> 

    <meta http-equiv="Content-Type" content="text/html; charset=windows-1255"> 
    <title>mange panel</title> 
    </head> 
    <body> 

    <br>Type value for test in ajax<input id="valueforajax" type=text name='ajaxtest'> 


    </body> 
    </html> 

回答

3

你錯過了#登陸您選擇器

$("valueforajax")應該$("#valueforajax")

data: dataString應該data: 'dataString=' + dataString OR data: {"dataString": dataString}因爲在你的PHP代碼,你正在尋找

$_POST['dataString']即用於dataString關鍵。

$("#valueforajax").blur(function() { // blur is perfect for what you searching 

    dataString = $.trim(this.value); // you don't need $("#valueforajax").val(); 
            // here, this is enough 

    if (dataString){ // checking for presence of value 
     $.ajax({ 
     type: "POST", 
     url: "../../ajax.php", 
      data: 'dataString=' + dataString, // or {"dataString": dataString} 
      cache: false, 
      success: function(html) { 
       alert("There is submited sucsses"); 
      } 
      }); 
     } 
    }); 
+0

你對,但仍然不工作的PHP代碼是否可以嗎? Thx – yossi

+0

@yossi檢查我的更新答案 – thecodeparadox

0

的數據必須是一個對象(如{"dataString" : "test"})或字符串(如"dataString=test"

1

你想用模糊來代替鼠標離開的。

每當您離開表單元素時都會發生模糊事件。當你按下TAB這樣的情況,請單擊該字段,改變字段等之外....

鼠標離開,只有當你的鼠標離開字面上的元素出現。

相關問題