gpt4 book ai didi

javascript - 该功能不会重定向到正确的位置并且警报不起作用

转载 作者:行者123 更新时间:2023-11-28 00:09:49 26 4
gpt4 key购买 nike

我尝试重定向到 sign.html#signperiod,它只重定向到页面本身

第二个问题警报没有做任何事情

我想重定向到 Aries.html#AriesMonth但目前它正在重定向到 Aries.html(显示在 url Aries.html#AriesMonth 中) 但不去<h1 id="AriesMonth" name="AriesMonth"/>的地方在白羊座的页面

第二个问题是警报不执行任何操作。如果我没有选择一个或两个单选按钮,我想激活一条警告消息,但目前它重定向到未定义的页面。为什么?

function redirectFunction() {
// var href = document.getElementById("sign").getAttribute('href');
var href = $("input[name='sign']:checked").val() + ".html";
window.location.href = href + "#" + $("input[name='sign']:checked").val() +
$("input[name='period']:checked").val();

if (href == "undefined.html" || $("input[name='sign']:checked") == false || $("input[name='period']:checked") == false)
alert("please select sign and and horoscope");
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js">
</script>


<div id="section">
<h3 for="horoscope">choose horoskop</h3>
<table>
<tr>
<td>
<input type="radio" name="period" value="Week" id="period" />
<b>שבועית</b>

</td>
<td>
<input type="radio" name="period" value="Month" id="period" />
<b>חודשית</b>

</td>
<td>
<input type="radio" name="period" value="Year" id="period">
<b>שנתית</b>

</td>
</tr>
</table>


<table>
<tr>
<th><input type="radio" value="Aries" name="sign" />
<a id="sign" href="Aries.html" />Aries</th>
</tr>

<tr>
<td>
<a name="signImage" value="Aries" href="Aries.html">
</tr>
</table>

<p><button onclick="redirectFunction()" id="redirect">get horoscope</button>
</p>

最佳答案

Period value is not validated, if period is not selected then it will be undefined and your # will be #signundefined. This is the reason behind not scrolling to # location.

Secondly, your location set happens 1st, even though sign value undefined it redirects. so put it inside if else.

 function redirectFunction() {
var value = $("input[name='sign']:checked").val(),
href = value + ".html";
if (href == "undefined.html" || $("input[name='sign']:checked") == false || $("input[name='period']:checked") == false) {
alert("please select sign and and horoscope");
} else {
window.location.href = href + "#" + value + $("input[name='period']:checked").val();
}

}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js">
</script>
<div id="section">
<h3 for="horoscope">choose horoskop</h3>
<table>
<tr>
<td>
<input type="radio" name="period" value="Week" id="period" checked/>
<b>שבועית</b>

</td>
<td>
<input type="radio" name="period" value="Month" id="period" />
<b>חודשית</b>

</td>
<td>
<input type="radio" name="period" value="Year" id="period">
<b>שנתית</b>

</td>
</tr>
</table>


<table>
<tr>
<th><input type="radio" value="Aries" name="sign" />
<a id="sign" href="Aries.html" />Aries</th>
</tr>

<tr>
<td>
<a name="signImage" value="Aries" href="Aries.html">
</tr>
</table>

<p><button onclick="redirectFunction()" id="redirect">get horoscope</button>
</p>

关于javascript - 该功能不会重定向到正确的位置并且警报不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55449588/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com