复选框选择表单重定向

时间:2013-05-10 09:15:38

标签: javascript html forms redirect

这是我想要实现的目标:我想创建一个带有表单的HTML页面,当您提交表单时,它会转到4个位置中的1个位置。在页面加载时有一个默认的隐藏主选项和2个可选的子选项。

哦,它计算选择的数量!

到目前为止,这是我的代码:

<html>
<head></head>
<body>

<form onSubmit="submitForm();" id="myForm" type="get">
<input id="myCheckbox1" name="myCheckbox1" type="checkbox" value="20" onClick="calcNow();" />Default option<br/>
<input id="myCheckbox2" name="myCheckbox2" type="checkbox" value="30" onClick="calcNow();" />Add-on option 1<br/>
<input id="myCheckbox2" name="myCheckbox2" type="checkbox" value="40" onClick="calcNow();" />Add-on option 2<br/>
<input id="myTotal" name="myTotal" type="text" value="" disabled="disabled" /><br/>
<input type="button" id="myButton" onClick="submitForm();" value="Continue" />
</form>

<script type="text/javascript">
var pages = [[["http://mysite.com/page1.html"],["http://mysite.com/page2.html"],["http://mysite.com/page3.html","http://mysite.com/page4.html"]]];

function calcNow()
{
    var cb = document.getElementById("myCheckbox1");
    var cb = document.getElementById("myCheckbox2");
    var cost1 = cb.checked ? parseInt(cb.value) : 0;
    var cost2 = cb.checked ? parseInt(cb.value) : 0;
    var costTotal = cost1 + cost2;
    document.getElementById("myTotal").value = costTotal;

    var op1 = cb.checked ? 1 : 0;

    if (op1 != undefined)
    {
        return pages[op1];
    }
    return undefined;
}

function submitForm()
{
    var page = calcNow();
    if (page != undefined)
    {
        alert(page);
        // ---- To navigate ----
        //location.href = page;
        // ---- To alter post ----
        //var form = document.getElementById("myForm");
        //form.action = page;
        //form.submit();
    }
    else
    {
        alert("Please answer all questions.");
    }
}

function getRadioValue(name)
{
  var controls = document.getElementsByName(name);
  for (var i = 0; i < controls.length; i++) {
      if (controls[i].checked) {
          return parseInt(controls[i].value);
      }
  }
  return 0;
}

function getRadioData(name, attribute)
{
  var controls = document.getElementsByName(name);
  for (var i = 0; i < controls.length; i++) {
      if (controls[i].checked) {
          return parseInt(controls[i].dataset[attribute]);
      }
  }
  return undefined;
}
</script>
</body>
</html>

1 个答案:

答案 0 :(得分:0)

试试这个

修改

function submitForm()
{
    //The code goes inside here, you have to decide where to redirect from if or the else 
    window.location.assign("http://www.w3schools.com/");   

    var page = calcNow();
    if (page != undefined)
    {
        alert(page);
    }
    else
    {
        alert("Please answer all questions.");
    }
}