2010-04-13 69 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