JavaScript-提交单选按钮后重定向到页面

时间:2019-01-23 22:04:26

标签: javascript html

我的页面上有两个单选按钮(Client和Owner),我希望用户如果单击Client按钮,则将其重定向到Client.html页面,如果单击Checking Owner按钮,则将其重定向到Owner.html。 这是我的代码:

function jump() {
  var select = document.getElementByName("user");
  for (var i = 0; i < selected.length; i++) {
    var value = selected[i].value;
    if (selected[i].checked) {
      if (value === 'Client') {
        window.location.href = 'Client';
      } else {
        window.location.href = 'Owner';
      }
    }
  }
}
<h1>Registration :</h1>
<script src="jump.js"></script>
<form>
  <p>Are you a Client or an Owner ?</p>
  <input type="radio" name="user" value="Client" checked> Client<br>
  <input type="radio" name="user" value="Owner"> Owner<br>
  <input type="button" onclick="jump()" value="OK">
</form>

其他.html文件客户端和所有者仅包含标题。 谢谢你。

2 个答案:

答案 0 :(得分:2)

您应该使用getElementsByName而不是getElementByName。 此外,您还命名了变量select,并尝试使用selected

对其进行引用

function jump() {
  var selected = document.getElementsByName("user");
  for (var i = 0; i < selected.length; i++) {
    var value = selected[i].value;
    if (selected[i].checked) {
      if (value === 'Client') {
        window.location.href = 'Client';
      } else {
        window.location.href = 'Owner';
      }
    }
  }
}
<h1>Registration :</h1>
<script src="jump.js"></script>
<form>
  <p>Are you a Client or an Owner ?</p>
  <input type="radio" name="user" value="Client" checked> Client<br>
  <input type="radio" name="user" value="Owner"> Owner<br>
  <input type="button" onclick="jump()" value="OK">
</form>

答案 1 :(得分:0)

您的JavaScript代码正在检查错误的内容-您应该尝试这样做:

function hump() {
    var client = document.querySelector("input[value='Client']");
    if (client.checked) {
        window.location.href = "Client.html";
    } else {
        window.location.href = "Owner.html";
    }
}