2012-06-19 19 views
0

原諒完整新手問題在這裏 - 我知道這是非常基本的東西,但對於我的生活我無法弄清楚。 Javascript對我來說是比較新的,這是我第一次不得不做這個特殊的事情。如何在JavaScript函數中使用傳遞的參數?

所以,我試圖使用一個模態來打開一個iframe - 頁面本身將鏈接到幾個模式,所有這些都需要通過一個不同的值。而不是硬編碼每個這些,我試圖設置它的方式,可以使用一個函數,並且鏈接可以根據需要傳遞值。

代碼我目前已經成功打開模式,但404錯誤是在裏面 - 加上模式標題顯示+標題+ - 所以我想我引用它錯誤(可能在函數?)。

繼承人我得到了,指針在正確的方向將不勝感激!

function openIframe(title,url){ 
    $.modal({ 
     title: '+title+', 
     url: '+url+', 
     useIframe: true, 
     width: 600, 
     height: 400 
    }); 
} 

..和鏈接:

<a href="#" onclick="openIframe('Process Voucher','a_processvoucher.cfm')">Add</a> 

回答

2

要使用變量,不說出來了; title正在從字面上設置爲字符串+title+(和url相同)。

function openIframe(title, url) { 
    $.modal({ 
     title: title, 
     url: url, 
     useIframe: true, 
     width: 600, 
     height: 400 
    }); 
}​ 

看起來你對連接語法感到困惑,無法連接字符串和變量;例如,看到以下內容:

var name = "Matt"; 
var welcome = "Hi " + name + ", how are you doing today?"; 
alert(welcome); 

...會提醒串Hi Matt, how are you doing today?

+0

謝謝!它對它進行了排序:-) – Lee

1

要引用名爲titleurl,值爲「標題」和「URL」

不是字符串變量
function openIframe(title,url) { 
    $.modal({ 
     title: title, // no quotes 
     url: url, // no quotes 
     useIframe: true, 
     width: 600, 
     height: 400 
    }); 
} 
相關問題