Pregunta

I'm creating a site that would allow a user to select one of any number of answers. Ultimately, I've got it all working to a point. The system works, but the submit doesn't seem to work if a user is on the page for over 30 seconds. There's no timeout set that should be causing this on the site, and I can't seem to find what would cause it in the jQuery .post. Here's the code that I'm using:

function submitAns(str, num) {
    var radios = document.getElementsByName(str);
    for (var i = 0, length = radios.length; i < length; i++) {
        if (radios[i].checked) {
            $.post('saveAnswer.aspx', {
                'answer': String(radios[i].value),
                'question': num,
                'comment': String($("#comment").val())
            }, function (msg) {
                if (msg == "failed") {
                    jAlert('Error Saving Data.');
                } else {
                    window.location = "Voting-Page";
                }
            });
        }
    }
}

This is hit after the user clicks on a button for a form that looks similar to this:

<input type='radio' name='group0' value='Yes'>Yes
<input type='radio' name='group0' value='No'>No
<textarea id='comment' rows='7' cols='40'></textarea>
<input type="submit" name="submitbtn" value="Submit" onclick="submitAns('group0','0');" class="btnSubmit" />

If I submit the form within the 30 second time frame it goes through without any problem. Otherwise, the page is reloaded and the form isn't submitted. I've tried adding a large timeout to the .post by adding 'timeout': 300000, but that still didn't work. I also tried removing the jAlert and checking for any site errors (none found). What could be the problem here?

¿Fue útil?

Solución

What I can see is there is a submit button in your page, the default action of the the submit button is to submit the form, to prevent it you can return false from the onclick event handler.

But what puzzles me is, in this case it has to consistent, the 30 second part does not have any effect here.

Any way try changing the onclick handler in submit button to

<input type="submit" name="submitbtn" value="Submit" onclick="submitAns('group0','0'); return false;" class="btnSubmit" />
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top