使用$ .post分配后,post值仍保留相同的值

时间:2012-11-03 07:47:15

标签: forms jquery

<script type="text/javascript">
      $(document).ready(function($) {
        $('#searchicon').click(function(){
            $('#searchicon.toappear').toggle();
            $('#searchicon2.appear').toggle();
            $('#extraSearch').slideToggle('fast');
        });
        $('#searchicon2').click(function(){
            $('#searchicon.toappear').toggle();
            $('#searchicon2.appear').toggle();
            $('#extraSearch').slideToggle('fast');
        });

        $('input[name=estimate]').click(function(){
            if (this.checked) {
                $("input.groupestimate").removeAttr("disabled");
            } else {
                $("input.groupestimate").attr("checked", false);
                $("input.groupestimate").attr("disabled", true);
            }
        });
        $("input.groupgatepass").attr("disabled", true);
        $('input[name=gatepass]').click(function(){
            if (this.checked) {
                $("input.groupgatepass").removeAttr("disabled");
            } else {
                $("input.groupgatepass").attr("checked", false);
                $("input.groupgatepass").attr("disabled", true);
            }
        });

        /*
        $('input.groupestimate').click(function() {
                    if(this.checked) {
                        $form = $('#globalSearch form');
                        if($('#globalSearch form[ACTION*="'+this.value+'"]'))
                        var data = new Object();
                          data.field = this.value;
                          var dataString = $.toJSON(data);
                                                      $.post('json.php',
                                 {data: dataString}, 
                                 function(res){ 
                                       var obj = $.evalJSON(res); 
                                       $form.attr('ACTION',$form.attr('action')+''+obj.query);
                                 }
                          );    
                    }

                });*/


        $('#globalSearch form').submit(function() {
            if($('input.groupheader[type=checkbox]:checked').length > 1) {
                alert('Please check only Estimate or Gatepass');
                return false;
            }

            var data = new Object();
            $('input.groupheader[type=checkbox]:checked').each(function(){
                data.header = this.value;
            });

            data.fields = new Array();
            $header = 'input.group'+data.header;
            $($header+'[type=checkbox]:checked').each(function(){
                data.fields.push(this.value);
            });

            var dataString = $.toJSON(data);
            $.post('json.php',
                   {data: dataString}, 
                   function(res){ 
                       var obj = $.evalJSON(res); 
                       $('input[name=query]').val(obj.query);
                   }
            );  
            alert($('input[name=query]').val()); //alerts nothing or the old value :(

            return false;
        });

      });
  </script>

------------- HTML PART -------------------

<form action="?<?php echo http_build_query(cloneGet(array("server-page"=>$GLOBALS["setting"]["current_folder"], "currpage"=>"0","OrderBy"=>"","ByDirect"=>"","search_flg"=>"-1")));?>" method="post" id="" name="">
    <div id="extraSearch">
        <table class="stripe" width="100%" id="extraTableList">
            <tbody>
                <tr class="striped">
                    <th style="cursor:pointer;text-align:left;"><input type="checkbox" name="estimate" value="estimate" checked="checked" class="groupheader">Estimates</th>
                    <th style="cursor:pointer;text-align:left;"><input type="checkbox" name="gatepass" value="gatepass" class="groupheader">Gatepass</th>
                </tr>
                <tr>
                    <td class="int"><input type="checkbox" name="estimate1" value="customer" class="groupestimate">Customer</td>
                    <td class="int"><input type="checkbox" name="gatepass1" value="old_serial" class="groupgatepass">Old Serial</td>
                </tr>
                <tr>
                    <td class="int"><input type="checkbox" name="estimate2" value="serial_no" class="groupestimate">Serial No</td>
                    <td class="int"><input type="checkbox" name="gatepass2" value="type" class="groupgatepass">Type</td>
                </tr>
                <tr>
                    <td class="int"><input type="checkbox" name="estimate3" value="invoice_no" class="groupestimate">Invoice No</td>
                    <td class="int"><input type="checkbox" name="gatepass3" value="type_id" class="groupgatepass">Type ID</td>
                </tr>
                <tr>
                    <td class="int"><input type="checkbox" name="estimate4" value="salesman" class="groupestimate">Salesman</td>
                    <td class="int"><input type="checkbox" name="gatepass4" value="loader" class="groupgatepass">Loader</td>
                </tr>
                <tr>
                    <td class="int"><input type="checkbox" name="estimate5" value="estimate_no" class="groupestimate">Estimate No</td>
                    <td class="int"><input type="checkbox" name="gatepass5" value="total" class="groupgatepass">Total</td>
                </tr>
                <tr>
                    <td class="int"><input type="checkbox" name="estimate6" value="status" class="groupestimate">Status</td>
                    <td class="int">&nbsp;</td>
                </tr>
             </tbody>
         </table>
    </div>
    <img id="searchicon" src="___images/search-arrow.png" class="toappear" alt="Extra search" title="Extra search"/>
    <img id="searchicon2" src="___images/search-arrow.png" class="appear" alt="Extra search" title="Extra search"/>





      <input type="text" value="<?php echo $_REQUEST['quicksearch'];?>" id="s" name="quicksearch">
      <input type="hidden" value="" id="query" name="query" temp="">
      <input type="submit" value="Search" id="b">

<?php if( $_REQUEST['quicksearch'] ) { ?>
      <a id="c" href="?<?php echo http_build_query(cloneGet(array("quicksearch"=>"","search_flg"=>"-1")));?>">Cancel</a>
<?php } ?>  

    </form>

如您所见,在提交值之前,我想在'query'输入元素值属性中插入一个字符串。在$ .post的回调中执行它时它非常有效,但是在使用$ .post之外的alert()检查它之后它是空的。

$ .post总是那样吗?还是我错过了什么?

1 个答案:

答案 0 :(得分:0)

当您使用 $。发布时,它会发出异步请求。警报的原因将不包含任何内容或旧值,因为它在$ .post完成其请求之前被触发。 当警报放在$ .post回调中时,它将包含您想要的数据,因为它在$ .post完成后被调用