admin管理员组

文章数量:1434924

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');"

本文标签: Javascript Passing variable to function breaks functionStack Overflow