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

javascript - Input button in firefox doesn't work - Stack Overflow

programmeradmin2浏览0评论

(I've changed the input tag with a button tag.) This probably is a stupid question, but here goes. I have an input button like this:

<button type="submit"
       id="choice"
       value="Escolher" 
       onClick="javascript:makeChanges()">

It works well in IE, but in firefox it just doesn't do anything; there are no errors, no behavior what so ever.

My "makeChanges()" function does the following:

var selObj = document.getElementById('opiniao');    
var selIndex = selObj.selectedIndex;
var str = selObj.options[selIndex].text +'&random='+(new Date()).getTime();
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.open("GET","writeSettings2.php?text="+str,true);
xmlhttp.send(null);
history.go(0);
}

If i put for isntance an alert on the call like:

<button type="submit"
           id="choice"
           value="Escolher" 
           onClick="alert('Hello')">

The button works, but it dosent when i try to call the javascript function.

(I've changed the input tag with a button tag.) This probably is a stupid question, but here goes. I have an input button like this:

<button type="submit"
       id="choice"
       value="Escolher" 
       onClick="javascript:makeChanges()">

It works well in IE, but in firefox it just doesn't do anything; there are no errors, no behavior what so ever.

My "makeChanges()" function does the following:

var selObj = document.getElementById('opiniao');    
var selIndex = selObj.selectedIndex;
var str = selObj.options[selIndex].text +'&random='+(new Date()).getTime();
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.open("GET","writeSettings2.php?text="+str,true);
xmlhttp.send(null);
history.go(0);
}

If i put for isntance an alert on the call like:

<button type="submit"
           id="choice"
           value="Escolher" 
           onClick="alert('Hello')">

The button works, but it dosent when i try to call the javascript function.

Share Improve this question edited Feb 3, 2011 at 15:21 Catarrunas asked Feb 3, 2011 at 14:38 CatarrunasCatarrunas 433 silver badges8 bronze badges 4
  • Look in the Firebug console for any JS errors, if it's not working there should be some feedback as to why. – David Thomas Commented Feb 3, 2011 at 14:39
  • 1 Have you tried ditching the "javascript:" in your onclick attribute? There's no need to specify that you're putting a script element in there. – Surreal Dreams Commented Feb 3, 2011 at 14:42
  • 1 the input is of type submit. without a form, firefox doesnt know what to submit. – Victor Commented Feb 3, 2011 at 14:42
  • @Victor: Your ment reads like an answer. Consider posting it. – Jeff Yates Commented Feb 3, 2011 at 14:43
Add a ment  | 

7 Answers 7

Reset to default 2

<input type="submit" /> is for submitting form data. Without <form> firefox doesn't know where or how the form should be submitted, so it doesn't submit.

If you want to use a button without a form, you can use the <button> tag.

try:

<input type="button" id="choice" value="Escolher" onClick="makeChanges()" />

what is your javascript? maybe there is a mistake?

Can you add some context about your code around this input element? Is it in a form (doesn't sound like it)? The "submit" input type needs to be in a form...can you just change it to a button input element instead?

<input type="button" id="choice" value="Escolher" onClick="javascript:makeChanges()">

The input is of type submit. Which indicates to submit a form. Without a form, Firefox doesnt know what to submit.

I just tried your code and it worked ok in Firefox 3. On the following HTML. Check if your function is defined. Or maybe it is your Firefox version?

<html>
<head>

<script type="text/javascript">

function makeChanges(){
    alert('hi');
}

</script>

</head>
<body>

<input type="submit"
       id="choice"
       value="Escolher"
       onClick="javascript:makeChanges()">

</body>
</html>

indeed this doesnt work on firefox (but works on IE, Chrome):

input type="button" value="Show Results" onClick="javascript:show()"

the solution is just put the javascript code inside the onClick, for instance:

input type="button" value="Show Results" onclick="document.getElementById('dsp').value=1;sertificatesStatsForm.submit();"

If you have the type as "submit", browsers will attempt to post data to the enclosing <form> element's "action" attribute. You can disable this by adding "return false;" to your onclick handler. As others have said, "button" is the more appropriate type to use in this case. The below bare-bones example works for me:

&lt;input type="button" onClick="javascript:alert('hello');" /&gt;

Do you perhaps have NoScript installed in Firefox? Normally, file:// is not allowed to execute JavaScript code.

发布评论

评论列表(0)

  1. 暂无评论