gpt4 book ai didi

javascript - 在单选按钮更改时

转载 作者:行者123 更新时间:2023-11-30 18:13:11 25 4
gpt4 key购买 nike

我有一组 2 个单选按钮,可以在更改时更改设备方向。

我遇到的问题是,当我选择横向单选按钮时,它起作用了,但是当我重新选择纵向单选按钮时,我收到横向警报,并且它保持不变。

这是我的尝试:

HTML:

<fieldset data-role="controlgroup">
<input type="radio" name="orientation" id="portrait" value="1" checked />
<label for="portrait">Portrait</label>

<input type="radio" name="orientation" id="landscape" value="2" />
<label for="landscape">Landscape Mode</label>
</fieldset>

JS:

$("input[name='orientation']").change(function() {
if ($("input[name='orienation']:checked").val() == '1') {
navigator.screenOrientation.set('portrait');
alert('portrait');
}
else if ($("input[name='orienation']:checked").val() == '2') {
navigator.screenOrientation.set('landscape');
alert('landscape');
}
})

我正在使用 jQuery 和 jQuery Mobile

最佳答案

您的 if 语句应该更像:

if ($(this).val() == '1') {
...
}
else if ($(this).val() == '2') {
...
}

关于javascript - 在单选按钮更改时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14073673/

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