首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >用JavaScript有条件地打开表单提交页面

用JavaScript有条件地打开表单提交页面
EN

Stack Overflow用户
提问于 2014-06-27 18:29:08
回答 3查看 2.8K关注 0票数 1

我试图在表单提交上设置一个条件页面加载,遵循“如果用户选择x选项,将它们发送到x页”逻辑。我真的不太熟悉JavaScript,但这似乎是最简单的方法。

这是我的HTML:

代码语言:javascript
复制
<form id="page1" action="javascript:OpenWindow()" method="post">
    <fieldset id="mainSelection">
            <label><input type="radio" class="radio-button" value="A" name="sel1"> text</label><br />
            <label><input type="radio" class="radio-button" value="B" name="sel1"> text</label><br />
            <label><input type="radio" class="radio-button" value="C" name="sel1"> text</label><br />
            <label><input type="radio" class="radio-button" value="D" name="sel1"> text</label><br />
    </fieldset>
    <button type="submit" value="Next" id="submitButton">Next</button>
</form>

这是我的剧本:

代码语言:javascript
复制
<script type="text/javascript" language="javascript">
        function OpenWindow() {
        var choice = document.getElementByClassName("radio-button").value
            if (choice == A) {
                window.open('http://www.website.net');
            }
            else if (choice == B) {
                window.open('http://www.website.net');
            }
            else if (choice == C) {
                window.open('http://www.website.net');
            }
            else {
                window.open('http://www.website.net');
            }
        }
</script>

我在这里做错什么了?

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2014-06-27 19:15:57

我做了一些改变。

  • 将onsubmit="OpenWindow()“添加到form元素
  • 更改获取所选元素的方式
  • 关于你的条件('A','B‘等)

下面是到代码库的链接:http://codepen.io/anon/pen/qajxh

看起来是这样的:

代码语言:javascript
复制
<form onsubmit="OpenWindow()" id="page1" action="javascript:OpenWindow()" method="post">
    <fieldset id="mainSelection">
            <label><input type="radio" class="radio-button" value="A" name="sel1"> text</label><br />
            <label><input type="radio" class="radio-button" value="B" name="sel1"> text</label><br />
            <label><input type="radio" class="radio-button" value="C" name="sel1"> text</label><br />
            <label><input type="radio" class="radio-button" value="D" name="sel1"> text</label><br />
    </fieldset>
    <button type="submit" value="Next" id="submitButton">Next</button>
</form>

像这样的js:

代码语言:javascript
复制
function OpenWindow() {
    var choice = document.getElementById("page1");
    choice = choice.sel1.value;

    if (choice == 'A') {
        window.open('http://www.website.net');
    }
    else if (choice == 'B') {
        window.open('http://www.website.net');
    }
    else if (choice == 'C') {
        window.open('http://www.website.net');
    }
    else {
        window.open('http://www.website.net');
    }
} 
票数 2
EN

Stack Overflow用户

发布于 2014-06-27 18:38:49

如果您的表单在没有javascript的情况下正常工作,那么为什么不尝试用Javascript更改forms操作,而不是告诉表单超出常规使用javascript打开窗口。

代码语言:javascript
复制
<form id="page1" action="http://www.website.net" method="post">
<fieldset id="mainSelection">
    <input type='radio' value='http://www.website.net' name='sel1' onclick='javascript: document.getElementById("page1").action = this.value;' />
    ...
</fieldset>
<input type='submit' name='submit' value='Submit' />
</form>

如果您曾经将单选按钮设置为默认值,则可以将默认操作设置为该按钮URL的窗体。

编辑(使用相同HTML的替代方法)

若要获取您所拥有的内容,请将您的javascript更改为:

代码语言:javascript
复制
<script type="text/javascript" language="javascript">
function OpenWindow() {
    // grab the elements by name (array)
    var choice = document.getElementsByName("radio-button");
    // loop through the array
    for (var i = 0; i < choice.length; i++ ) {
        // grab only the checked one
        if (choice[i].checked) {
            if (choice[i].value == A) {
                window.open('http://www.website.net');
            }
            else if (choice[i].value == B) {
                window.open('http://www.website.net');
            }
            else if (choice[i].value == C) {
                window.open('http://www.website.net');
            } else {
                window.open('http://www.website.net');
            }
        }
    }
}
</script>
票数 0
EN

Stack Overflow用户

发布于 2014-06-27 18:46:25

我觉得这不像你想的那样。我也不能测试这一点,但我很确定调用getElementByClassName("radio-button")将返回所有4个标签,因为它们都有类radio-button

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/24458634

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档