0

我正在尝试将单选按钮表单中的值添加到我的数据库中,但我的 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> 
4

3 回答 3

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;
};
于 2013-05-10T11:33:46.050 回答
0

从 JS 函数中删除以下行:

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

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

于 2013-05-10T11:34:00.240 回答
0

您可以检查是否有任何复选框被选中

if( $('input[name=tvshowrating]:checked')[0]) { //will return true if checked element exists }
于 2013-05-10T11:35:59.187 回答