如何在javascript的循环中从表单中选择特定的隐藏元素


how to select specfic hidden element from a form in a loop in javascript?

所以基本上,我的循环输出四次下面的html,在$show_id中有不同的值。当调用函数时,Everyform返回第一个表单的$show_id值,而不是表单的值。如何解决这个问题?

我javascript

    <script type="text/javascript" >
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").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(data){
                $("#flash").html('Added');
            }
        });
    }
    return false;
};
        </script>

myhtml

     <div id="flash"></div>
        <form id="form3B">
            <div class="your-score">
                <div class="">Your Score</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>
                 <input id="submitscore" type="submit" value="Submit scores!" onclick="addScore()" />  
            </div>
        </form>
    </div>
</div>

这是因为你不能有重复的id,不像重复的类,你需要为每个表单不同的id。像这样

<input type="hidden" id="show_id-form3B" value="<?php echo $row[0]; ?>" /> 

那么你就有可能得到每个不同形式的唯一值。像这样的

function addScore(formId) {
var show_id = $('#show_id-'+formId).val();  
var user_id = $('#user_id-'+formId).val();
//add your code
}