最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - a href onclick not working on some pages - Stack Overflow

programmeradmin6浏览0评论

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

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?

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

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?

Share Improve this question edited Jun 15, 2015 at 12:06 StudioTime asked Jan 2, 2012 at 7:33 StudioTimeStudioTime 24k40 gold badges128 silver badges215 bronze badges
Add a ment  | 

5 Answers 5

Reset to default 8

Wrap the content of the onclick attribute in double-quotes (");

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

Just to be clear, it's not that attributes require double-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>
  • Not OK: <a href="#" onclick='do('this');'>bar</a>
  • Not OK: <a href="#" onclick="do("this");">bar</a>

Here is your problem;

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

Use regular quotes for you HTML arguments and you´ll be fine.

As it seems you´re using PHP to create a string with your HTML you´d try this to escape the quotes;

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

its not able to detect form name because that too is in single quotes.

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

fiddle : http://jsfiddle/QFe3L/

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;"
<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>
发布评论

评论列表(0)

  1. 暂无评论