獲得html表單中radio name相同的陣列的值 並判斷每個選項不能為空

tian_fighting發表於2017-10-09
<form method="post" action="" οnsubmit="return aa()">

    {foreach from="$info" item="fee"}
    <!--問題:-->{$fee.id}、{$fee.question}<br>
<label class="ui-radiobox" rel="common_user_type">
    <input type="radio" name="score{$fee.id}" value="{$fee.a_score}" >  A:{$fee.a_choice}    
</label>
<label class="ui-radiobox" rel="common_user_type">

    <input type="radio" name="score{$fee.id}" value="{$fee.b_score}">  B:{$fee.b_choice}    
</label>
    <label class="ui-radiobox" rel="common_user_type">

    <input type="radio" name="score{$fee.id}" value="{$fee.c_score}">  C:{$fee.c_choice}    
    </label>
        <label class="ui-radiobox" rel="common_user_type">

    <input type="radio" name="score{$fee.id}" value="{$fee.d_score}">  D:{$fee.d_choice}
        </label>
    <br>
    {/foreach}
    <input type="submit">
</form>

jq程式碼

<script type="text/javascript">
    function aa(){
        var err_msg='';
        var arr_name=[];
        $("input:radio[name^=score]").each(function(){
            if (!arr_name[this.name]){
                arr_name[this.name]=1;
            }
        });

        for (name in arr_name)
        {
            //alert(name);   //score8   score9  score7  score6
            //console.log($("input:radio[name="+name+"]").val());
            if ($("input:radio[name="+name+"]:checked").length==0)
            {
                var names = name.substr(5);
                //alert(names);
                err_msg+="第"+names+"題還沒選擇答案\r\n";
            }

            if (err_msg!="")
            {
                alert(err_msg);
                return false;
            }
        }
</script> 



相關文章