2010-04-13 117 views
0

嘿所有,我一直在這個問題上嘗試幾分鐘,我似乎無法弄清楚如何糾正它。當它涉及函數中的多個可變參數時,我傾向於最難做這樣的事情。javascript onclick函數字符串問題

下面是代碼:

var tempString = "thePrompt('Are you sure you wish to delete this?', 'theDel('" + IDnum + "', '" + theTitle + "', \'" + temperDay + "\', \'" + temperMonth + "\', \'" + temperYear + "\', \'" + DDiff + "\')"; 

html += 
"<div id='theTable" + IDnum + "' class='fc-event fc-event-hori fc-corner-left fc-corner-right' style='position:absolute; margin-top: 15px; z-index:8;left:"+left+"px'><div id='apDEL' style='position:relative;width:0px;height:0px;z-index:1000; top:-13px; left:2px; float:left;'><img src='img/xCal.png' width='15' height='15' alt='' border='0' onclick=\"" + tempString + "\" /></div>" + (ETC ETC...) 

我不斷收到錯誤是這樣的:

Error: missing) after argument list 
Line: 1, Column: 68 
Source Code: 
thePrompt('Are you sure you wish to delete this posting?', 'theDel('105', '50 points for visiting us today!!!!', '13', '3', '2010', '2') 

而且它指向 '105'。

一如既往,任何幫助都會很棒! :O)

大衛

編輯/ UPDATE

好了,現在我已經移出了一些東西,我得到它的工作(即發送值,在彈出的提示框,但對於一些原因,它不看我的功能我按下刪除鍵後!這是因爲,提示是主要的頁面上,我是有工作的代碼是一個iframe內。

function theDel(theID, theTitle, day, month, year, DDiff) 
{ 
    var tempString = "delClientE(" + theID + ", '" + theTitle + "', " + day + ", " + month + ", " + year + ", " + DDiff + ")"; 
    parent.thePrompt('Are you sure you wish to delete this event?', tempString); 
} 

刪除按鈕值現在看像這樣:

delClientE(110, 'sadfsadfsdf', 14, 3, 2010, 2); jQuery.prompt.close(); 

然而,即使把parent.delClientE犯規找到函數或者...我怎樣才能把它從iframe中時,提示框是是麼?

大衛

回答

0

我終於明白了!好極了! :o)

function theDel(theID, theTitle, day, month, year, DDiff) 
{ 
    var tempString = "window.frames.theCal.delClientE(" + theID + ", '" + theTitle + "', " + day + ", " + month + ", " + year + ", " + DDiff + ")"; 
    parent.thePrompt('Are you sure you wish to delete this event?', tempString); 
} 

我需要window.frames.theCal來調用我的函數。 theCal是我的iframe ID。

大衛

1

我認爲這是缺少在聲明的末尾..你的聲明是這樣的:

var tempString = "thePrompt('question', theDel(params)"; 

..它應該有一個更加封閉「)」結尾。

還有一件事你可能想看看的事實是,在德爾的參數被包裹在單引號。

thePrompt('Are you sure you wish to delete this?', 'theDel('105', '50 points for visiting us today!!!!', '13', '3', '2010', '2')') 

而且,由於在thePrompt的PARAMS也被包裹在單引號,所以我會做thePrompt有雙引號角落找尋它的PARAMS所以它會是這個樣子,可能是一個問題:

var tempString = "thePrompt(\"Are you sure you wish to delete this?\", \"theDel(\'" + IDnum + "\', \'" + theTitle + "\', \'" + temperDay + "\', \'" + temperMonth + "\', \'" + temperYear + "\', \'" + DDiff + "\')\")";  
+0

剛試過,雅各指出,但是我仍然得到同樣的錯誤。錯誤:缺失)參數列表後 行:1,列:68 源代碼: thePrompt('您確定要刪除此?','theDel('105','50分今天訪問我們! '','13','3','2010','2')') – StealthRT 2010-04-13 21:37:46

+0

將其更改爲您建議的最後一個給我的另一個錯誤: 錯誤:語法錯誤 行:1,列:9 源代碼: thePrompt( – StealthRT 2010-04-13 21:57:55

+0

是你的函數字符串中的參數嗎?他們都是? – drusnov 2010-04-13 22:26:15

2

這是因爲這裏的引號'theDel('" +你應該逃避內部的單引號。 編輯。當然,除了桌面上已經存在的缺失的右派問題之外。 :)

4

跟蹤嵌套上下文中的轉義真的很難,因爲當你弄錯了,你經常給自己一個腳本注入安全問題,最好避免通過粘附字符串來創建HTML/JS一起。

相反,使用DOM方法從JavaScript分配的事件處理程序,告別一切刺激性反斜線和引用的東西:

var img= document.createElement('img'); 
img.src= 'img/xCal.png'; 
img.width=img.height= 15; 
img.onclick= function() { 
    if (confirm('Are you sure you wish to delete this?')) 
     theDel(IDnum, theTitle, temperDay, temperMonth, temperYear, DDiff); 
}; 
div.appendChild(img); 
+0

我試過了,它說div沒有被定義... – StealthRT 2010-04-13 22:59:23

+0

@StealthRT:你必須用元素來代替'div' nt添加新創建的'img'。例如,爲了追加到body,你可以使用'document.body.appendChild(img);'。 – 2010-04-13 23:20:20

+0

問題在於xCal.png是一個用delete或cancel按鈕拋出提示的按鈕,因此我不真正知道在彈出的提示符後面追加到刪除按鈕的內容(或如何)。 – StealthRT 2010-04-13 23:27:58

0

大衛,不知道這已回答讓您滿意呢,但它可能是更容易地創建一個臨時變量此:

所有的
var theDel = "theDel('" + IDnum + "', '" + theTitle + "', '" + temperDay + "', '" + temperMonth + "', '" + temperYear + "', '" + DDiff + "')"; 
    var tempString = "thePrompt('Are you sure you wish to delete this?', \' + theDel + \')"; 
+0

這種方式實際上彈出提示,但刪除按鈕的值不正確......它onclick =「+ theDel +; jQuery.prompt.close();」當然,錯誤出現在第二個「+」號上。 – StealthRT 2010-04-13 22:27:49

0

首先,你會好得多使用event delegation with the js library of your choice

做不到這一點,一個字符串插值功能,使這些事情更容易:

String.prototype.format = function(values) { 
    return this.replace(/\{(.+?)\}/g, function(s, key) { 
    return values[key]; 
    }); 
} 

只要這樣的事情成爲:

var s = 'blah blah blah "{IDnum}" blah blah blah \'{foosball}\''.format({ IDnum: '1', foosball: 'xyz'}); 

(如果你不喜歡操縱String的原型,你可以把功能放在別的地方。)

0

好吧,這個怎麼樣?

的iFrame源

<html> 
<head> 
<script type="text/javascript"> 
    function localDel(params){ 
     if (window.parent && typeof window.parent.thePrompt==='function') 
      window.parent.thePrompt('Are you sure you wish to delete this event?', params); 
    } 
</script> 
</head> 
<body> 
    <p onclick="localDel([1,'some title', 14, 3, 2010, 2])">delete</p> 
    <p onclick="localDel([2,'another title', 14, 3, 2010, 2])">delete</p> 
</body> 
</html> 

父源

<html> 
<head> 
    <script type="text/javascript"> 

    function thePrompt(msg,params) { 
     if(confirm(msg)) theDel.apply(this,params); 
    } 

    function theDel(id, title, tDay, tMonth, tYear, dDiff) { 
     alert(id); 
     // etc... 
    } 

    </script> 
</head> 
<body> 
    <iframe src="iframe.html" /> 
</body> 
</html> 
+0

錯誤在這個以及...錯誤:語法錯誤 行:1,列:50 源代碼: thePrompt('您確定要刪除此?', – StealthRT 2010-04-13 23:09:49

+0

我已經刪除了行提要,應該 – Jonathan 2010-04-14 00:03:16

+0

似乎並不奏效,它切斷了theDel。thePrompt中的所有內容('您確定要刪除嗎?', – StealthRT 2010-04-14 01:13:06