使用$ .post方法两次

时间:2014-07-02 10:21:11

标签: php jquery ajax

是否可以使用jquery.post为2级我的意思是(secod $ .post取决于第一个)

让我们假设公司有电话号码,姓名和描述(为了这个例子)

<body>

    // retrieving companies that have a name similar (or contains )
    // what is written in the input box bellow :

    <form method="POST" id="searchForm">
       <input type="text" name ="name" id="name">
       <input type="submit" value="Find">
    </form>

    // when the user enter a name in the input and submit the form
    // with out refreshing the page (adding return false to the jquery click function) 
    // we will display all retrieved company code inside <ul>(using loop for example)

    <ul id="companies_list">

      <li id="company"><input type="hidden" value="[company code goes here ]>
      <span>[company name goes here]</span><li>
    </ul>

        // the user select  the desired company name (li item )
        // and then without refreshing the page we will display the information
       // about the selected company in the company_details area

    <div id="company_details">
         [here goes the selected company details]
    </div>

jquery代码:

$(document).ready(function(){
// when the dom is ready start manipulating 
$("#searchForm").submit(function(event){
 // stop form from being submited normaly
  event.preventDefault();

// sendig post data
var name = $("input[type="text"]").val();
 var url = "ajax/search.php";
 var posting = $.post( url, {name:name} );

posting.done(function(data) {
  // assuming the data holds companies list (ie: <li>c1</li><li>c2</li>)
  $("#companies_list").html(data);  
});


});



// the last part (below)  does'not work for me

$("#company").click(function(){
    var code = [ company code stored in hidden field]
    var url = 'ajax/detail';
    var posting1 = $.post(url,{code:code});
    posting1.done(function(data){
      put the result in the company_details div
      $("#company_details").html(data)
    });
    return false;
});

});

1 个答案:

答案 0 :(得分:0)

是的,$.post的第三个参数是成功完成发布后执行的函数。所以代码是:

$(document).ready(function(){
// when the dom is ready start manipulating 
$("#searchForm").submit(function(event){
 // stop form from being submited normaly
  event.preventDefault();

// sendig post data
var name = $("input[type="text"]").val();
 var url = "ajax/search.php";
 var posting = $.post( url, {name:name}, function(data) {
  // assuming the data holds companies list (ie: <li>c1</li><li>c2</li>)
  $("#companies_list").html(data);  
} )
});