2013-05-10 120 views
0

我想從一個單選按鈕的形式添加到我的數據庫的值,但我的JavaScript不會返回任何錯誤,它不工作。我認爲我的選擇器可能不工作,但我該如何檢查?我的功能有什麼問題?如何檢查值是否正確選擇(或不)通過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> 

回答

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; 
}; 
+0

這樣的作品,除了閃光燈股利不顯示進度檢查你可以檢查? – CharlieAus 2013-05-10 11:39:29

+0

@CharlieAus它是什麼都沒有顯示 – 2013-05-10 11:44:24

+0

不,它不是... – CharlieAus 2013-05-10 11:51:33

0

刪除以下行來自JS的功能:

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

並關閉它,因爲您已通過提交按鈕上的單擊事件調用它。

0

如有複選框被

if($('input[name=tvshowrating]:checked')[0]) { //will return true if checked element exists } 
+0

謝謝你,我會試試看! – CharlieAus 2013-05-10 11:52:15

相關問題