javascript a href onclick 在某些页面上不起作用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8698451/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
a href onclick not working on some pages
提问by Darren Sweeney
I have the following code, i use v similar code on many pages but on this particular page it simply doeasn't work in either ie or ff
我有以下代码,我在许多页面上使用了类似的代码,但在这个特定页面上,它在 ie 或 ff 中都不起作用
here's the code:
这是代码:
<form action='/productView.html' method=post name=prod_form>
<a href='javascript:;' onclick='document.forms['prod_form'].submit(); return false;' class='button101' style='margin-left:92px;'>".$button_text."</a>
<input type=hidden name=action value='".$button_text."'>
<input type=hidden name=PRid value=".$databack3[PRid].">
<INPUT type='hidden' name='cat_id' value=".$databack3[prodcatID].">
<INPUT type='hidden' name='for_user_id' value=".$for_user_id.">
<input type=hidden name=source value=".$source."></form>
However, if i change to normal button as below it works fine:
但是,如果我更改为如下所示的普通按钮,它可以正常工作:
<form action='/productView.html' method=post name=prod_form>
<input type=submit name=action class=button99 value='".$button_text."' style='margin-left:85px;'
<input type=hidden name=PRid value=".$databack3[PRid].">
<INPUT type='hidden' name='cat_id' value=".$databack3[prodcatID].">
<INPUT type='hidden' name='for_user_id' value=".$for_user_id.">
<input type=hidden name=source value=".$source."></form>
Any ideas?
有任何想法吗?
回答by Ayman Safadi
Wrap the content of the onclick
attribute in double-quotes (");
将onclick
属性的内容用双引号 (") 括起来;
onclick="document.forms['prod_form'].submit(); return false;"
Just to be clear, it's not that attributes requiredouble-quotes, but you can't have the same kind of quotes inside your outer quotes, unless you escape them.
需要明确的是,并不是属性需要双引号,而是您不能在外部引号内使用相同类型的引号,除非您将它们转义。
For example:
例如:
- OK:
<a href="#" onclick="do('this');">bar</a>
- OK:
<a href="#" onclick='do("this");'>bar</a>
- OK:
<a href="#" onclick="do(\"this\");">bar</a>
- OK:
<a href="#" onclick='do(\'this\');'>bar</a>
- NotOK:
<a href="#" onclick='do('this');'>bar</a>
- NotOK:
<a href="#" onclick="do("this");">bar</a>
- 行:
<a href="#" onclick="do('this');">bar</a>
- 行:
<a href="#" onclick='do("this");'>bar</a>
- 行:
<a href="#" onclick="do(\"this\");">bar</a>
- 行:
<a href="#" onclick='do(\'this\');'>bar</a>
- 不正常:
<a href="#" onclick='do('this');'>bar</a>
- 不正常:
<a href="#" onclick="do("this");">bar</a>
回答by Stefan
Here is your problem;
这是你的问题;
onclick='document.forms['prod_form'].submit(); return false;'
Use regular quotes for you HTML arguments and you′ll be fine.
为您的 HTML 参数使用常规引号,您会没事的。
As it seems you′re using PHP to create a string with your HTML you′d try this to escape the quotes;
看起来您正在使用 PHP 创建一个带有 HTML 的字符串,您可以尝试使用它来转义引号;
onclick=\"document.forms['prod_form'].submit(); return false;\"
回答by dku.rajkumar
its not able to detect form name
because that too is in single quotes
.
它无法检测到,form name
因为它也在single quotes
.
<a href='#' onclick='document.forms["prod_form"].submit(); return false;'
class='button101' style='margin-left:92px;'>".$button_text."</a>
fiddle : http://jsfiddle.net/QFe3L/
回答by Hari viswadeep
It is just because of using single quotes. Just change your
这只是因为使用单引号。只需改变你的
onclick='document.forms['prod_form'].submit(); return false;'
to
到
onclick="document.forms['prod_form'].submit(); return false;"
回答by Boopathi
<b><form action='/productView.html' method=post name=prod_form> <a
href='javascript:;' onclick="document.forms['prod_form'].submit();
return false;" class='button101'
style='margin-left:92px;'>".$button_text."</a> <input type=hidden
name=action value='".$button_text."'> <input type=hidden name=PRid
value=".$databack3[PRid]."> <INPUT type='hidden' name='cat_id'
value=".$databack3[prodcatID]."> <INPUT type='hidden'
name='for_user_id' value=".$for_user_id."> <input type=hidden
name=source value=".$source."></form>
Hope it works
</b>