Javascript: Passing variable to function breaks function - Stack Overflow

I am using an onsubmit variable to ensure that the user really means to delete something, however as so

I am using an onsubmit variable to ensure that the user really means to delete something, however as soon as I put a value in the parenthesis inside the onsubmit it no longer calls the confirm box.

Code:

onClick="confirmSubmit(abc)"

Doesn't work but the following:

onClick="confirmSubmit()"

Does work

Function:

    function confirmSubmit(category)
{
var category = category;
var agree=confirm("Are you sure you wish to DELETE" + category + " and all of its subcategories and photos?");
if (agree)
    return true ;
else
    return false ;
}

I am using an onsubmit variable to ensure that the user really means to delete something, however as soon as I put a value in the parenthesis inside the onsubmit it no longer calls the confirm box.

Code:

onClick="confirmSubmit(abc)"

Doesn't work but the following:

onClick="confirmSubmit()"

Does work

Function:

    function confirmSubmit(category)
{
var category = category;
var agree=confirm("Are you sure you wish to DELETE" + category + " and all of its subcategories and photos?");
if (agree)
    return true ;
else
    return false ;
}
Share Improve this question asked Jul 20, 2011 at 18:55 George ReithGeorge Reith 13.5k18 gold badges82 silver badges151 bronze badges 1
  • confirm() returns a boolean, so you can shorten your return statement by just returning the return value of the confirm. return confirm("Are you sure...");. – user113716 Commented Jul 20, 2011 at 19:00
Add a ment  | 

4 Answers 4

Reset to default 5

you need quotes around your abc:

onclick="confirmSubmit('abc')"

Without them you are trying to pass a variable, abc, which doesn't exist and triggers an error

onClick="confirmSubmit(abc)" is trying to pass the variable abc, if you intend to pass a string with the value "abc" then do this:

onClick="confirmSubmit('abc')"

function confirmSubmit(category)  
{  var category = category;  

And you've declared "category" twice! Once in the function header and then as a function variable in the next line! What for?

You're try to pass the variable abc (which does not exist) to the function. Do: onclick="return confirmSubmit('abc');"

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745644201a4637872.html

相关推荐

  • Javascript: Passing variable to function breaks function - Stack Overflow

    I am using an onsubmit variable to ensure that the user really means to delete something, however as so

    22天前
    60

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信