如何正确获取单选按钮值

时间:2013-12-06 23:41:41

标签: javascript radio-button

我需要一些帮助才能获得几个单选按钮的值。我起初似乎已经正确地完成了它,但现在却抛出了内置的错误。

代码:http://pastie.org/8534504

1 个答案:

答案 0 :(得分:0)

试试这个:

        var wgServer = mw.config.get("wgServer");

        function powerpagemaker() {
            var wgPageName = mw.config.get("wgPageName"),
                pagetitle = "New_Page";

            if (wgPageName !== pagetitle) {
                return;
            }

            var html = '<form style="width: 75%;"><fieldset><legend>Create a New Page</legend><input type="radio" name="namespace" value="main" checked="checked">Main</input><input type="radio" name="namespace" value="blog">Blog</input><input type="radio" name="namespace" value="category">Category</input><input type="radio" name="namespace" value="mediawiki">MediaWiki</input><input type="radio" name="namespace" value="template">Template</input><br/><br/><input type="text" id="NewPageTitle" style="width: 80%;" placeholder="Page title"/><br/><br/><span>Page Content: </span><input type="radio" name="pagecontent" value="blank" checked="checked">Blank</input><input type="radio" name="pagecontent" value="populated">Populated</input><br/><br/><button type="button" onclick="makenewpage()" style="float: right;">Create</button></fieldset></form>';

            document.getElementById("WikiaArticle").innerHTML = html;
        }

        function makenewpage() {
            var namespace,
                pagetosend = document.getElementById("NewPageTitle").value || "NULL",
                content;

            if (pagetosend == "NULL") {
                alert("Please give the name of a page to create");
            }

            var radioname = document.getElementsByName("namespace"),
                radiocont = document.getElementsByName("pagecontent");

            for (var i = 0; i < radioname.length; i++) {
                if (radioname[i].checked) {
                    namespace = radioname[i].value;
                    break;
                }
            }

            for (var i = 0; i < radiocont.length; i++) {
                if (radiocont[i].checked) {
                    content = radiocont[i].value;
                    break;
                }
            }

            var link;
            switch (namespace) {
                case "main":
                    if (content == 'populated') {
                        link = 'http://' + wgServer + '/wiki/' + pagetosend + '?action=edit&useFormat=1';
                    } else {
                        link = 'http://' + wgServer + '/wiki/' + pagetosend + '?action=edit';
                    }
                    break;
                case "blog":
                    link = 'http://' + wgServer + '/wiki/Special:CreateBlogPage';
                    break;
                case "category":
                    link = 'http://' + wgServer + '/wiki/Category:' + pagetosend + '?action=edit';
                    break;
                case "mediawiki":
                    link = 'http://' + wgServer + '/wiki/MediaWiki:' + pagetosend + '?action=edit';
                    break;
                case "template":
                    link = 'http://' + wgServer + '/wiki/Template:' + pagetosend + '?action=edit';
                    break;
                default:
                    alert('ERROR');
                    return;
            }
            window.location = link;
        }

        $(document).ready(function () {
            powerpagemaker()
        });