开发者

Input button in firefox doesn't work

(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
开发者_Go百科  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.


<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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜