2012-02-27 78 views
0

我有下面的鏈接的jQuery/AJAX的幫助需要

<div id='list1'> 
<a href='#1'>USA</a> 
<a href='#2'>Canada</a> 
<a href='#3'>France</a> 
</div> 

<div id='list2'> 
<a href='#1'>Cars</a> 
<a href='#2'>Houses</a> 
<a href='#3'>Machines</a> 
</div> 

我想要做的是當美國用戶點擊它,然後(同時,美國的鏈接)得到大膽的名單。然後用戶點擊列表2中的任何元素。單擊兩個鏈接後,鏈接1和鏈接2中所選元素的值將傳遞給ajax函數。任何人都可以告訴我這件事。

+0

什麼如果用戶首先點擊第二個列表中的鏈接,然後點擊第一個鏈接中的鏈接? – Alp 2012-02-27 11:12:09

+0

你到目前爲止嘗試過什麼嗎?也許發佈代碼片段。別指望別人爲你做這項工作 – Bogdan 2012-02-27 11:13:25

+0

發表一下你的javascript,我們可以幫你 – Awea 2012-02-27 11:14:19

回答

1

我沒有檢查它,但你可以使用水木清華這樣的:

<div id='list1'> 
<a href='#1'>USA</a> 
<a href='#2'>Canada</a> 
<a href='#3'>France</a> 
</div> 

<div id='list2'> 
<a href='#1'>Cars</a> 
<a href='#2'>Houses</a> 
<a href='#3'>Machines</a> 
</div> 

<script> 
    $(document).ready(function(){ 
      $('#list1 > a').click(function(){ 
       $('#list1 > a').attr('class', ''); 
       $(this).attr('class','selected'); 

       if ($('#list2 > a.selected').length > 0) 
       { 
        $.post('post/submit/url', {'link1':$(this).attr("href"), 'link2':$('#list2 > a.selected').attr("href")}, function(data){ 
          alert(data); 
        }); 
       } 
      }); 

      $('#list2 > a').click(function(){ 
       $('#list2 > a').attr('class', ''); 
       $(this).attr('class','selected'); 

       if ($('#list1 > a.selected').length > 0) 
       { 
        $.post('post/submit/url', {'link1':$('#list1 > a.selected').attr("href"), 'link2':$(this).attr("href")}, function(data){ 
          alert(data); 
        }); 
       } 
      }); 
    }); 
</script> 
+0

有想法..謝謝 – 2012-02-27 11:26:15

1

做這樣的事情: 用於存儲所選國家的值創建一個隱藏的輸入元素,

 

$("#list1 a").click(function(e) { 
    e.preventDefault(); 
    $(this).addClass("blodAnchor");// may be font-weight bold in this class 
    $("#selectedCountry").val(''); 
    $("#selectedCountry").val($(this).text()); 
}); 

//then 
$("#list2 a").click(function(e) { 
    e.preventDefault(); 
    var selectedVal = $(this).text() || ""; 
    var selectedCountry = $("#selectedCountry").val() || ""; 
    if(selectedCountry != "" && selectedVal != "") { 
    $.ajax({ 
     type: "POST", 
     data: {country: selectedCountry, someVal : selectedVal}, 
     url: ".....", 
     success: function(resp) { 
      ........ 
     } 
    }); 
    } 
});