[英]JavaScript code in a tag onclick
我需要做一些丑陋的事情。 但是我不能以其他方式做到这一点。 我需要的是
<a href='some/url/?with¶ms' onclick="(if confirm('Do you wanna submit?')
{some code to submit form in href property})">
但我不知道如何使内联脚本正常工作...并且我对使用window.location或document.location提交脚本的方式感到犹豫。 有任何想法吗?
尝试这个:
在html上:
<a href='some/url/?with¶ms' onclick="if (confirm('Do you wanna submit?')) return MyValidation.actionSubmit();">Submit</a>
<script type="text/javascript" src="myValidation.js" />
myValidation.js
我在这个答案上使用json和javascript 闭包 。
; //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;
}
};
看一下这个:
您听说过AJAX吗? 谷歌,如果你不这样做。 onClick参数接受带有或不带有参数的函数名,而不是“内联脚本”。
<script type=text/javascript>
function doStuff(){
if(confirm("Submit?"){
//do your stuff with AJAX to some/url/?with¶ms
}
}
</script>
<a href='javascript:doStuff'>link</a>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.