html - Form submit button onClick JavaScript function - Stack Overflow

I made a simple form with radio buttons and submit button. On submit, check() function is called and it

I made a simple form with radio buttons and submit button. On submit, check() function is called and it prints out text dependent on which radio button is selected.

But, it only works when I submit for the second time. First time I sumbit, page refreshes, ?req=on is added in the url, and that's it.

Why is that, and how can I get it to work every time I press the submit button?

HTML:

<form name="request" onsubmit="return check();">
    <p>
        <label>Pick fragments: </label>
    </p>
    <fieldset>
        <input id="rally" type="radio" name="req" />
        Longest rally
        <br />
        <input id="gamept" type="radio" name="req" />
        Game points
        <br />
        <input id="setpt" type="radio" name="req" />
        Set points
        <br />
        <br />
        <input type="submit" />
    </fieldset>
</form>

<ul id="uri"></ul>

And part of the script:

function check() {

    window.location.hash = "article6";
    var uri = new Array();
    var vid = new Array();
    $('#uri').empty();

    if($('#rally')[0].checked) {

        //get json with the longest rally and print it out
        $.getJSON('json/longRally.json', function(json) {
            for(var i = 0; i < json["results"]["bindings"].length; i++) {
                uri[i] = json["results"]["bindings"][i]["longRally"].value
                vid[i] = json["results"]["bindings"][i]["uriRally"].value

            };

            uri.sort(alphanum);
            vid.sort(alphanum);

            for(var i = 0; i < json["results"]["bindings"].length; i++) {
                $('#uri').append('<li><a href="' + vid[i] + '">' + uri[i] + '</a></li>');
            };
        });
    }   
}

Web is located here:

.0/#article6

I made a simple form with radio buttons and submit button. On submit, check() function is called and it prints out text dependent on which radio button is selected.

But, it only works when I submit for the second time. First time I sumbit, page refreshes, ?req=on is added in the url, and that's it.

Why is that, and how can I get it to work every time I press the submit button?

HTML:

<form name="request" onsubmit="return check();">
    <p>
        <label>Pick fragments: </label>
    </p>
    <fieldset>
        <input id="rally" type="radio" name="req" />
        Longest rally
        <br />
        <input id="gamept" type="radio" name="req" />
        Game points
        <br />
        <input id="setpt" type="radio" name="req" />
        Set points
        <br />
        <br />
        <input type="submit" />
    </fieldset>
</form>

<ul id="uri"></ul>

And part of the script:

function check() {

    window.location.hash = "article6";
    var uri = new Array();
    var vid = new Array();
    $('#uri').empty();

    if($('#rally')[0].checked) {

        //get json with the longest rally and print it out
        $.getJSON('json/longRally.json', function(json) {
            for(var i = 0; i < json["results"]["bindings"].length; i++) {
                uri[i] = json["results"]["bindings"][i]["longRally"].value
                vid[i] = json["results"]["bindings"][i]["uriRally"].value

            };

            uri.sort(alphanum);
            vid.sort(alphanum);

            for(var i = 0; i < json["results"]["bindings"].length; i++) {
                $('#uri').append('<li><a href="' + vid[i] + '">' + uri[i] + '</a></li>');
            };
        });
    }   
}

Web is located here:

http://b-webdesign./multilab/Ver3.0/#article6

Share Improve this question edited Sep 7, 2011 at 17:46 3mpetri asked Sep 7, 2011 at 17:19 3mpetri3mpetri 5991 gold badge9 silver badges26 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 4

Add:

return false;

To the end of your function.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742343987a4426175.html

相关推荐

  • html - Form submit button onClick JavaScript function - Stack Overflow

    I made a simple form with radio buttons and submit button. On submit, check() function is called and it

    6小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信