一个href onclick不能在某些页面上工作

时间:2012-01-02 07:33:30

标签: javascript html forms onclick

我有以下代码,我在许多页面上使用v类似的代码,但在这个特定的页面上它只是在ie或ff

中都不起作用

这是代码:

<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>

但是,如果我更改为正常按钮,如下所示,它可以正常工作:

<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>

有什么想法吗?

5 个答案:

答案 0 :(得分:8)

用双引号(“);

包装onclick属性的内容
onclick="document.forms['prod_form'].submit(); return false;"

为了清楚起见,不是属性需要双引号,但你不能在外引号中使用相同类型的引号,除非你将它们转义。

例如:

  • 好的:<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>

答案 1 :(得分:4)

这是你的问题;

onclick='document.forms['prod_form'].submit(); return false;'

为你的HTML参数使用常规引号,你会没事的。

由于您似乎正在使用PHP创建一个包含HTML的字符串,因此请尝试使用它来转义引号;

onclick=\"document.forms['prod_form'].submit(); return false;\"

答案 2 :(得分:3)

它无法检测到form name,因为它也在single quotes

<a  href='#' onclick='document.forms["prod_form"].submit(); return false;' 
           class='button101' style='margin-left:92px;'>".$button_text."</a>

小提琴:http://jsfiddle.net/QFe3L/

答案 3 :(得分:1)

这只是因为使用单引号。 只需改变你的

onclick='document.forms['prod_form'].submit(); return false;'

onclick="document.forms['prod_form'].submit(); return false;"

答案 4 :(得分:0)

<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>
相关问题