2011-12-28 12 views
0

如何根據用戶更新文本框中的數字來調用Ajax?我如何使用jQuery獲取字段和另一個字段的值併發送給ajax?

我有類似以下內容。我刪除了周圍的代碼,以便更容易地遵循。我在這裏展示了一個更大的網格的兩行。

  <div class="rep_tr0"> 
       <div class="rep_td0" id="2">0001</div>    
       <div class="rep_td0"><input id="position_1" name="position_1" size="5" type="text" value="0" /></div>       
      </div> 
      <div class="rep_tr0"> 
       <div class="rep_td0" id="2">0002</div>   
       <div class="rep_td0"><input id="position_2" name="position_2" size="5" type="text" value="0" /></div> 
      </div> 

我需要當用戶更改名爲「position_x」輸入字段的一個是那麼我就需要

- get the value of the input field marked with "position_x" 
- get the value that is stored in the div with id="x" 
- make an ajax call passing the two parameters. 

注意,X標記的行號。

回答

1

事情是這樣的:

 

$("input[id^='position_']").change(function() { 
    //get the value of the input field marked with "position_x" 
    var posX = $(this).val(); 
    var idArr = $(this).attr("id"); 
    var idTmp = idArr.split("_"); 
    var id = idTmp[1]; 
     var divX = $("div[id='"+id+"']").html();  
    make an ajax call passing the two parameters. 
    $.ajax({ 
     type: "POST", 
     url : "your_url", 
     data: "pos_x="+posX+"&div_x="+divX, 
     success: function(response) { 
      alert(response); 
     } 
    }); 
}); 
 
+0

感謝。我會試試這個。看起來有點複雜,但也許沒有別的辦法。 – 2011-12-28 07:48:39

相關問題