代码有什么问题(重定向哪个href)

时间:2019-03-29 19:52:48

标签: javascript jquery html

我有这个功能:

function redirect() {
  document.getElementById("starSign").getAttribute('href');
  window.location.href = href + "#" + $("input[name='sign']:checked").val() +
    $("input[name='period']:checked").val();
}
<table>
  <tr>
    <td>
      <input type="radio" name="period" value="Week" id="week" />
      <b>weekly</b>
    </td>
  </tr>

  <tr>
    <td>
      <table cellpadding="10">
        <tr>
          <th><input type="radio" value="Aries" name="sign" id="Aries" /> Aries</th>
        </tr>
        <tr>
          <td>
            <a id=starSign value="Aries" href="Aries.html"> <img src="Aries.jpg" alt="Aries" /></a>
          </td>
        </tr>


<button onclick=redirect()>click here to get the horoscope</button>

我尝试通过该函数重定向到Aries.html#AriesWeek,但没有任何反应。我也可以redirect(href)吗?

1 个答案:

答案 0 :(得分:0)

因为您获得了href值,但是您从未将其存储到任何变量中以备后用。

function redirect() {
  var href = document.getElementById("starSign").getAttribute('href');
  window.location.href = href + "#" + $("input[name='sign']:checked").val() +
    $("input[name='period']:checked").val();
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<table>
  <tr>
    <td>
      <input type="radio" name="period" value="Week" id="week" />
      <b>weekly</b>
    </td>
  </tr>

  <tr>
    <td>
      <table cellpadding="10">
        <tr>
          <th><input type="radio" value="Aries" name="sign" id="Aries" /> Aries</th>
        </tr>
        <tr>
          <td>
            <a id=starSign value="Aries" href="Aries.html"> <img src="Aries.jpg" alt="Aries" /></a>
          </td>
        </tr>


<button onclick=redirect()>click here to get the horoscope</button>