0

I want to enter value, to input field so when I leave the input field by clcking out side from the input field I will run the ajax code and send the value input dataString to same file which in this case cald ajax.php. Thanks

my code:

$(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>
4

3 回答 3

3

You missed # sign in you selector

$("valueforajax") should be $("#valueforajax").

also

data: dataString should be data: 'dataString=' + dataString OR data: {"dataString": dataString} because in your php code you're searching for

$_POST['dataString'] i.e. for dataString key.

$("#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");
             }
          });
        }
    });
于 2012-05-28T15:28:28.280 回答
1

您想使用模糊而不是鼠标离开。

每当您离开表单元素时都会发生模糊事件。当您点击 TAB、在字段外单击、更改字段等时会发生这种情况......

mouseleave 仅在您的鼠标从字面上离开元素时发生。

于 2012-05-28T15:43:13.457 回答
0

数据必须是对象(例如{"dataString" : "test"})或字符串(例如"dataString=test"

于 2012-05-28T15:38:09.370 回答