如何通过javascript正确选择(或不)正确选择值?

时间:2013-05-10 11:30:07

标签: php javascript ajax

我正在尝试将单选按钮表单中的值添加到我的数据库中但是我的javascript不会返回任何错误而且它无法正常工作。我认为我的选择器可能无法正常工作,但我该如何检查呢?我的功能出了什么问题?

JS

<script type="text/javascript" >
    function addScore() {
    $("#submitscore").click(function() 
    {
    var show_id = $('#show_id').val();  
    var user_id = $('#user_id').val();
    var score = $('input[name=tvshowrating]:checked').val();
    if(score=='')
    {
    alert('PleaseEnter A Score');
    }
    else
    {
    $("#flash").show();
    $("#flash").fadeIn(400).html('<img src="ajax-loader.gif" />Loading Score...');
    $.ajax({
    type: "POST",
    url: "showscoreajax.php",
            data:{
            "show_id" : show_id,  
            "user_id" : user_id,
            "score" : score          //we are passing the name value in URL
            },
    cache: false,
    success: function(html){
    $("#flash").html('Added');
    }
    });
    }return false;
    }); 
    };


    </script>

HTML

<form id="form3B">


    <div class="your-score">
        <div class="">Your Score</div>
        <div id="flash"></div>
         <input class="hover-star" type="radio" name="tvshowrating" value="1" title="1"/>
         <input class="hover-star" type="radio" name="tvshowrating" value="2" title="2"/>
         <input class="hover-star" type="radio" name="tvshowrating" value="3" title="3"/>
         <input class="hover-star" type="radio" name="tvshowrating" value="4" title="4"/>
         <input class="hover-star" type="radio" name="tvshowrating" value="5" title="5"/>
         <input class="hover-star" type="radio" name="tvshowrating" value="6" title="6"/>
         <input class="hover-star" type="radio" name="tvshowrating" value="7" title="7"/>
         <input class="hover-star" type="radio" name="tvshowrating" value="8" title="8"/>
         <input class="hover-star" type="radio" name="tvshowrating" value="9" title="9"/>
         <input class="hover-star" type="radio" name="tvshowrating" value="10" title="10"/>    
         <input type="hidden" id="show_id" value="<?php echo $row[0]; ?>" /> 
         <input type="hidden" id="user_id" value="<?php echo $user_id ?>" />
         <span id="hover-test" style="margin:0 0 0 20px;"></span>
    </div>
    </div></div>
       <input id="submitscore" type="submit" value="Submit scores!" onclick="addScore()" />  
       <u>Test results</u>:<br/><br/>
       <div class="test Smaller">
        <span style="color:#FF0000">Results will be displayed here</span>
       </div>

    </form> 

3 个答案:

答案 0 :(得分:1)

$("#submitscore").click(function()方法中无需使用addSote(),因为它点击了按钮

function addScore() {
    var show_id = $('#show_id').val();  
    var user_id = $('#user_id').val();
    var score = $('input[name=tvshowrating]:checked').val();
    if(!score)
    {
        alert('PleaseEnter A Score');
    }
    else
    {
        $("#flash").show();
        $("#flash").fadeIn(400).html('<img src="ajax-loader.gif" />Loading Score...');
        $.ajax({
            type: "POST",
            url: "showscoreajax.php",
            data:{
                "show_id" : show_id,  
                "user_id" : user_id,
                "score" : score          //we are passing the name value in URL
            },
            cache: false,
            success: function(html){
                $("#flash").html('Added');
            }
        });
    }
    return false;
};

答案 1 :(得分:0)

从JS函数中删除以下行:

$("#submitscore").click(function() 
    {

并关闭它,因为你已经通过提交按钮上的点击事件来调用它。

答案 2 :(得分:0)

您可以检查

是否选中了任何复选框
if( $('input[name=tvshowrating]:checked')[0]) { //will return true if checked element exists }