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

I'm trying to add a value from a radio button form into my DB but my javascript doesn't return any errors and it's not working. I think my selector might not be working but how do I check that ? What's wrong with my function ?

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个回答

There is no need to use $("#submitscore").click(function() in the addSote() method since it called on click on the button

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;
};
doutan1637
doutan1637 我做了,它没有改变任何东西。 但我的形式是在foreach循环中,我认为问题可能来自于此。
7 年多之前 回复
doupang5433
doupang5433 你能尝试一次删除fadeIn吗?
7 年多之前 回复
duanhao4909
duanhao4909 不,什么都没有出现
7 年多之前 回复
doujiazong0322
doujiazong0322 是消息已添加显示
7 年多之前 回复
dongyin8991
dongyin8991 不,这不对...
7 年多之前 回复
dongzhang8475
dongzhang8475 是否显示任何东西
7 年多之前 回复
drh78568
drh78568 这有效,除了Flash div没有显示进度?
7 年多之前 回复



从JS函数中删除以下行:</ p>

  $(“#submitscore  “).click(function()
{
</ code> </ pre>

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

展开原文

原文

Remove the following lines from the JS function:

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

and also closing of it as you are already call it by click event on the submit button.



您可以通过</ p>

 检查是否有任何复选框if($('  input [name = tvshowrating]:checked')[0]){//如果选中元素存在则返回true} 
</ code> </ pre>
</ div>

展开原文

原文

You could check if any checkbox is checked by

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

doujiexin1136
doujiexin1136 谢谢你,我会试一试!
7 年多之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐