最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

Javascript: Passing variable to function breaks function - Stack Overflow

matteradmin7PV0评论

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

Post a comment

comment list (0)

  1. No comments so far