简体   繁体   English

Javascript onclick函数未触发

[英]Javascript onclick function not triggering

I have a rating system which I've used for several things on my website which doesn't seem to be working now. 我有一个评分系统,我已经在我的网站上使用了几种方法,但现在似乎无法使用。 It reloads the page instead of passing the values to my other php file. 它重新加载页面,而不是将值传递给我的其他php文件。

Can be seen there: 可以在那里看到:

http://broadcasted.tv/show/96/graceland/4213/season/1/episode/3/ http://broadcasted.tv/show/96/graceland/4213/season/1/episode/3/

<form class="episoderating" id="4213">
    <div class="your-score">
        <div class="" style="float:left">Your Score:</div> <span class="star-rating-control"><div class="rating-cancel" style=""><a title="Cancel Rating"></a></div><div role="text" aria-label="1" class="star-rating rater-0 hover-star star-rating-applied star-rating-live"><a title="1">1</a></div><div role="text" aria-label="2" class="star-rating rater-0 hover-star star-rating-applied star-rating-live"><a title="2">2</a></div><div role="text" aria-label="3" class="star-rating rater-0 hover-star star-rating-applied star-rating-live"><a title="3">3</a></div><div role="text" aria-label="4" class="star-rating rater-0 hover-star star-rating-applied star-rating-live"><a title="4">4</a></div><div role="text" aria-label="5" class="star-rating rater-0 hover-star star-rating-applied star-rating-live"><a title="5">5</a></div><div role="text" aria-label="6" class="star-rating rater-0 hover-star star-rating-applied star-rating-live"><a title="6">6</a></div><div role="text" aria-label="7" class="star-rating rater-0 hover-star star-rating-applied star-rating-live"><a title="7">7</a></div><div role="text" aria-label="8" class="star-rating rater-0 hover-star star-rating-applied star-rating-live"><a title="8">8</a></div><div role="text" aria-label="9" class="star-rating rater-0 hover-star star-rating-applied star-rating-live"><a title="9">9</a></div><div role="text" aria-label="10" class="star-rating rater-0 hover-star star-rating-applied star-rating-live"><a title="10">10</a></div></span>
        <input style="float: left; display: none;" class="hover-star star-rating-applied" type="radio" name="episoderating-4213" value="1" title="1">
        <input style="float: left; display: none;" class="hover-star star-rating-applied" type="radio" name="episoderating-4213" value="2" title="2">
        <input style="float: left; display: none;" class="hover-star star-rating-applied" type="radio" name="episoderating-4213" value="3" title="3">
        <input style="float: left; display: none;" class="hover-star star-rating-applied" type="radio" name="episoderating-4213" value="4" title="4">
        <input style="float: left; display: none;" class="hover-star star-rating-applied" type="radio" name="episoderating-4213" value="5" title="5">
        <input style="float: left; display: none;" class="hover-star star-rating-applied" type="radio" name="episoderating-4213" value="6" title="6">
        <input style="float: left; display: none;" class="hover-star star-rating-applied" type="radio" name="episoderating-4213" value="7" title="7">
        <input style="float: left; display: none;" class="hover-star star-rating-applied" type="radio" name="episoderating-4213" value="8" title="8">
        <input style="float: left; display: none;" class="hover-star star-rating-applied" type="radio" name="episoderating-4213" value="9" title="9">
        <input style="float: left; display: none;" class="hover-star star-rating-applied" type="radio" name="episoderating-4213" value="10" title="10">
        <input type="hidden" id="episode_id-4213" value="96">
        <input type="hidden" id="user_id-4213" value="2">
        <input id="submitscore-4213" type="submit" class="small button" value="Score!" onclick="EpisodeFunction(4213);">
    </div>
</form>

And the javascript 和JavaScript

function EpisodeFunction(formId) {
    var show_id = $('#episode_id-' + formId).val();
    var user_id = $('#user_id-' + formId).val();
    var episode_id = formId;
    var score = $('input[name="episoderating-' + formId + '"]:checked').val();
    if (!score)
    {
        alert('Score this show');
    }
    else
    {
      $('#show_id-' + formId).addClass('success');
        $.ajax({
            type: "POST",
            url: "/ajax/episodescoreajax.php",
            data: {
                "show_id": show_id,
                "user_id": user_id,
                "episode_id":episode_id,
                "score": score          //we are passing the name value in URL
            },
            cache: false,
            success: function(html)                {
                    if (html === "") {
                       $('#submitscore-' + formId).addClass('success');
                       $('#submitscore-' + formId).val('Scored!');
                       $('#submitscore-' + formId).attr('onclick', 'removeEpisodeScore(' + formId + ')');
                    }
                }
        });
    }
    return false;
};

I've used the function in other parts of the website without issue. 我已经在网站的其他部分使用了该功能,而没有出现任何问题。

Try to make your submit button to 尝试使您的提交按钮

<input id="submitscore-4213" type="button" class="small button" value="Score!" onclick="EpisodeFunction(4213);">

OR use form onsubmit 或使用表单提交

<form class="episoderating" id="4213" onsubmit="return EpisodeFunction(4213);">

AND remove onclick 并删除onclick

<input id="submitscore-4213" type="submit" class="small button" value="Score!">

Hopefully this helps.. 希望这会有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM