2010-04-27 135 views
2

我試圖在<a>上找到一個.click()來發現它不會在每次點擊時觸發,它假設要做的是打開一個對話框。每次使用.click()觸發?

這不是我需要將值傳遞給我的jquery的唯一問題。我只是不知道這一個。

我需要它是一個<a>因爲它不在下拉菜單中。你有什麼建議嗎?

編輯 這是我使用的代碼,它workes

$(document).ready(function() { 
     $('#dialog').dialog({ autoOpen: false, bgiframe: true, modal: true, height: 600, width: 1000, Close: function() { $(this).dialog('destroy'); } }); 

     $('a').live('click', function(evt) { 
      evt.preventDefault(); 
      var ids = $(this).attr('id'); 
      var first = "<iframe style='width: 100%; height: 100%;' src='" + "" + "'</iframe>'"; 
      $('.iframe').html(ids); 

      $('#dialog').dialog('open'); 
     }); 
    }); 

此代碼intilise對話框,打開每點擊它的工作每次,招我在這裏是在「摧毀」在結束和inilise

謝謝你們的幫助

回答

1

這將觸發對每一次點擊。雖然我懷疑這將是這種情況,但請檢查您是否沒有任何其他處理'a'的事件處理程序,它們正在應用event.stopImmediatePropagation()。嘗試添加return false的點擊處理程序結束,甚至更好:

$('a').click(function(evt) { 
     var first = "<iframe style='width: 100%; height: 100%;' src='" + need to put value here + "'</iframe>'"; 
     $('.iframe').html(first); 
     $('#dialog').dialog({ bgiframe: true, modal: true, height: 600, width: 1000 }); 

     evt.preventDefault(); 
    }); 

你可能會發現,如果你不阻止錨標記的默認操作,這將給人留下的頁面重新加載什麼都沒有發生。

你在想什麼樣的「價值」?您可以使用jQuery的data()來存儲信息,當然您可以訪問該範圍內的所有全局變量。

編輯: 爲了回答您的意見,您可以檢索點擊事件裏面一的ID如下:

var theIdOfTheA = $(this).attr('id'); 

注意,這必須放在處理程序中。

EDIT2:

$('a').live('click', function(evt) { 
     var first = "<iframe style='width: 100%; height: 100%;' src='" + $(this).attr('id') + "'</iframe>'"; 
     $('.iframe').html(first); 
     $('#dialog').dialog({ bgiframe: true, modal: true, height: 600, width: 1000 }); 

     evt.preventDefault(); 
    }); 
+0

每一個「一」消失了具有價值的ID和該值我想傳遞給點擊() – 2010-04-27 10:35:16

+0

見我的編輯.... – Matt 2010-04-27 10:40:41

+0

十分感謝馬特 – 2010-04-27 10:44:51

0

您可以通過執行(從jQuery docs)以下數據傳遞到事件處理程序:

$('#foo').bind('custom', function(event, param1, param2) { 
    alert(param1 + "\n" + param2); 
}); 
$('#foo').trigger('custom', ['Custom', 'Event']); 

所以,你可以這樣做:

$(document).ready(function() { 
    $('a').bind("click", function(event, myValue) { 
     var first = "<iframe style='width: 100%; height: 100%;' src='" + myValue + "'</iframe>'"; 
     $('.iframe').html(first); 
     $('#dialog').dialog({ bgiframe: true, modal: true, height: 600, width: 1000 }); 
    }); 
}); 

請注意,您現在使用bind()來綁定事件處理程序,並且該函數正在接收一個值(將其命名爲任何您想要的)。要在iframe顯示谷歌:

$('a').trigger('click', ['http://www.google.com']); 
+0

我沒有得到這樣的代碼,我應該如何使用觸發器?因爲我走了就像20-30'a',他們走了不同的值,應該使用。 – 2010-04-27 10:41:10

1

DEMO:http://jsbin.com/olalu/edit

它再次確保你接近對話框之前開啓!

<a href='javascript:;' id='my_unique_id' >click</a> 

EDITED

$('a').click(function(evt) { 
    evt.preventDefault(); 

    var theIdOfTheA = this.id; 

    var first = "<iframe style='width: 100%; height: 100%;' src='" + 
       "http://www.sf.se" + "'</iframe>'"; 
     $('.iframe').html(theIdOfTheA); 
     $('#dialog').dialog({ 
     bgiframe: true, 
     modal: true, 
     height: 600, 
     width: 1000, 
     //this 
     Close: function() { $(this).dialog('close'); }, 
     //OR this OR both 
     Cancel: function() { $(this).dialog('close'); } 
    }); 
}); 
+0

這可能是爲了關閉,但代碼是不正確的。它現在不會打開它 – 2010-04-27 11:22:45

+0

檢查編輯的一個! – 2010-04-27 11:25:40

+0

工作打開它是我錯了我錯過了一個},仍然不會與對話壽兩次火。 – 2010-04-27 11:32:28