0

我需要做一些丑陋的事情。但我不能以其他方式做到这一点。我需要的是

<a href='some/url/?with&params' onclick="(if confirm('Do you wanna submit?')
    {some code to submit form in href property})">  

但我不知道如何使内联脚本工作......我对使用 window.location 或 document.location 提交它的方式犹豫不决。有任何想法吗?

4

2 回答 2

1

尝试这个:

在 html 上:

<a href='some/url/?with&params' onclick="if (confirm('Do you wanna submit?')) return MyValidation.actionSubmit();">Submit</a>

<script type="text/javascript" src="myValidation.js" />

myValidation.js

我在这个答案上使用了

; //starts with this to close other js not yet closed
var MyValidation = {

    actionSubmit : function() {

            //your code go here

            //example of calling another function
            MyValidation.anotherFunction();

            //example of accessing a global variable (for your validation)
            MyValidation.variable;

            //example of declaring local variable
            var word = "hello";

            //example of calling another function with parameters
            MyValidation.anotherFunctionWithParameters(word, MyValidation.variable);

            //the returning
            return MyValidation.someValidation(word);
    }

    //Yes, this is a comma. 
    //I'm putting in the beginning so we see this and didn't forget :)
    ,anotherFunction : function() {

            //some code
            //maybe a return
    }

    ,variable : {}

    ,anotherFunctionWithParameters : function(param1, param2) {

            //more code, maybe a return
    }

    ,someValidation : function(parameter) {
        //some validation
        return true|false;
    }

};

看看这个:

  1. JavaScript 闭包是如何工作的?
于 2013-01-25T11:33:01.567 回答
0

你听说过 AJAX 吗?如果你不这样做,请谷歌它。onClick 参数接受带或不带参数的函数名称,而不是“内联脚本”。

<script type=text/javascript>
  function doStuff(){
      if(confirm("Submit?"){
       //do your stuff with AJAX to some/url/?with&params

     }
  }
</script>
<a href='javascript:doStuff'>link</a>
于 2013-01-25T11:40:29.300 回答