基于单选按钮值的JavaScript重定向

时间:2014-08-12 19:36:23

标签: javascript html wordpress if-statement radio-button

我正在检查单选按钮设置的值,看起来像"星级评分"系统并基于检查将用户重定向到特定页面。

但是下面的代码总是在else语句中结束。我想知道是否因为单选按钮设置为display:none;这会影响我的代码吗?

JavaScript添加到WordPress插件:

on_sent_ok: "if (document.getElementByName('review_stars').value=='4' || document.getElementByName('review_stars').value=='5'){location = 'http://my_domain/review-submission/'} else { location = 'http://my_domain/review-thank-you/'};"

这是HTML:

<span id="review_stars" class="wpcf7-form-control wpcf7-starrating wpcf7-validates-as-required review_stars starrating" data-cancel="0" data-def="0">
<span id="mf113" class="star-rating-control">
<div id="mf114" class="rating-cancel">
<div id="mf115" class="star-rating rater-0 star-rating-applied star-rating-live" aria-label="" role="text">
<div id="mf116" class="star-rating rater-0 star-rating-applied star-rating-live" aria-label="" role="text">
<div id="mf117" class="star-rating rater-0 star-rating-applied star-rating-live" aria-label="" role="text">
<a id="mfa23" title="3">3</a>
</div>
<div id="mf118" class="star-rating rater-0 star-rating-applied star-rating-live" aria-label="" role="text">
<div id="mf119" class="star-rating rater-0 star-rating-applied star-rating-live" aria-label="" role="text">
</span>
<input class="star-rating-applied" type="radio" value="1" name="review_stars" style="display: none;">
<input class="star-rating-applied" type="radio" value="2" name="review_stars" style="display: none;">
<input class="star-rating-applied" type="radio" value="3" name="review_stars" style="display: none;">
<input class="star-rating-applied" type="radio" value="4" name="review_stars" style="display: none;">
<input class="star-rating-applied" type="radio" value="5" name="review_stars" style="display: none;">
</span>

2 个答案:

答案 0 :(得分:1)

试试这个(注意复数“元素”)

document.getElementsByName();

此函数将返回与该名称匹配的元素数组(因此您可能必须使用索引来访问元素)。

如果您确定页面上只有一个元素,您可以尝试以下方法:

document.getElementsByName('review_stars')[0].value;

答案 1 :(得分:0)

由于您正在查看单选按钮,因此您可能需要以下内容:

if (document.getElementsByName('review_stars')[3].checked ||
    document.getElementsByName('review_stars')[4].checked) {    
      location = 'http://my_domain/review-submission/'} 
else { ... }