0

我有一个文件 json.js 和一个 php 函数在 php 文件 .in json.js 我想检查 php 函数返回的值,如果函数返回的值为 0 jquery 应该执行:$(':input').prop('禁用',真);否则什么都没有——</p>

function loadJson (table, id) {

    $.get("json-object.php", {'table': table, 'id':id}, function (data) {


        console.log(data);

        $.each(data, function (k, v) {
            if ($('input[name="'+k+'"]').is('input[type="text"]')) {
                $('input[name="'+k+'"]').val(v);
            } 
            if($('select[name="'+k+'"]').val(v)){
                get_input_value(k,v);   

            } 


            if ($('input[name="'+k+'"]').is('input[type="checkbox"]')) {
                get_input_value(k,v);
            } 
         console.log(k+' ==> '+v);


        // Here I want to check condition of php function if value returned by fucntion is 0 it should perform :$(':input').prop('disabled', true); otherwise nothing //
        });
    }, 'json');
}

我的php函数:

function ronly($id) {

    //$id=$_POST['noces'];
    $sql = "SELECT COUNT(noces) FROM alterdetail WHERE noces = '$id'";
    $sql.=';';
    //echo "QUERY <br/>";
    //echo $sql;
    $res = mysql_query($sql);
    $row = mysql_fetch_array($res);

           if($row['COUNT(noces)'] > 0)
                { echo "you can not alter data";
                return 0;
                }
                else
                {
                    echo "   data new  ";
                return 1;
                }
   }    
4

2 回答 2

2

你不能,因为 Javascript 是客户端执行的,而 PHP 是服务器端执行的......

“解决方案”是将 Javascript 变量分配到您将读入 Javascript 文件的 PHP 文件中,因为变量是全局的。

于 2012-06-20T07:55:56.413 回答
1

Use jQuery if possible.

$('#result').load('ajax/test.php', function() {
    alert('Function called');
});

Or try JQuery forms. Use a form to submit any data, and it'll give you the response as a text or JSon object.

http://jquery.malsup.com/form/#ajaxSubmit

Here is an example for you:

$('#anyForm').ajaxForm({
    url: "process.php?proc=7",
    dataType:  'html', 

    success: function(responseText) {
        if(responseText == "0") { 
            $(':input').prop('disabled', true); 
        }
    } 
});
于 2012-06-20T07:59:16.723 回答