簡體   English   中英

加載頁面后如何設置單選按鈕的默認值?

[英]how to set the default value of radio buttons after loading page?

我使用OpenERP 7,並且有2個單選按鈕可以更改表格的值。 我創建了一個調用python函數的事件,該事件執行sql查詢,之后我必須重新加載頁面以顯示帶有新值的表。 但是問題在於單選按鈕采用默認值! 因此用戶只有一個選擇,因此我必須在重新加載頁面后設置單選按鈕的默認值。 這是我的Qweb代碼:

<t t-if="number === 2">
        <table  style="margin-left:360px;margin-top:0px;margin-bottom:0px;">
            <tr>
                <td style="text-align:left;font-size:12px;"><b>Jusqu'au mois courant:</b></td>
                <td>
                   <input type="radio" id="radio_budget_cumulee_jusque_mois_courant" value="courant" name="radio_budget_cumulee" checked="checked"/>

                </td> 
            </tr> 
             <tr>      
                 <td style="text-align:left;font-size:12px;"><b>Jusqu'au fin d'année:</b></td>
                <td>   
                   <input type="radio" id="radio_budget_cumulee_jusque_fin_anneee" value="fin" name="radio_budget_cumulee"/>
                </td> 
            </tr>
        </table>
       <div id="itkbudg"> 
        <div  t-attf-id="#{view.element_id}_action_#{column_index}_#{action_index}" class="oe_content" t-att-style="action.attrs.fold ? 'display: none;' : 'height: 220px;overflow-x: hidden;overflow-y: auto;padding: 0 12px 1px;'"></div>
        </div> 
    </t>

這是我的Javascript代碼:

    $("#radio_budget_cumulee_jusque_mois_courant, #radio_budget_cumulee_jusque_fin_anneee").change(function(){


            est_jusqu_mois_courant=self.$el.find('#radio_budget_cumulee_jusque_mois_courant').is(':checked')
            est_jusqu_finannee=self.$el.find('#radio_budget_cumulee_jusque_fin_anneee').is(':checked')


            var mod=new instance.web.Model("itk.compta.dashboard");
            mod.call("itk_compta_dashboard_fct_changer_vue",[est_jusqu_mois_courant],{}).done(function(res) {



            });

            self.do_action({
                type: 'ir.actions.client',
                tag: 'reload',
        });



           // $radios.filter('[value=fin]').prop('checked', est_jusqu_finannee);
            //$radios.filter('[value=courant]').prop('checked', est_jusqu_mois_courant);


            //I tried to use this code
            var $radios = $('input:radio[name=radio_budget_cumulee]');
            $radios.filter('[value=fin]').attr('checked', est_jusqu_finannee);
            $radios.filter('[value=courant]').attr('checked', est_jusqu_mois_courant);
            // ANd i tried this code
            radiobtnmois_courant = document.getElementById("radio_budget_cumulee_jusque_mois_courant");
            radiobtnmois_courant.checked = false;

            radiobtnfin_anneee = document.getElementById("radio_budget_cumulee_jusque_fin_anneee");
            radiobtnfin_anneee.checked = true;

        });

但是總是單選按鈕采用默認值。 請任何幫助?

嘗試<input type="radio" id="..." autocomplete="off" />

並在您的腳本標簽中嘗試這樣的事情

$(document).ready(function(){
    $(".myradios").prop("checked", true); // true or false
});

為什么要重新加載孔頁面? 更好的方法是通過AJAX而不是孔頁重新加載所需的數據。 您可以通過JQuery甚至AngularJS將新值分配給表單

對於單選按鈕或復選框,您可以使用.val()使用值數組。

$radios.val( [ ] ); // uncheck all radios
$radios.val( [ "fin" ] ); // check the radio with value "fin"
$radios.val( [ "courant" ] ); // check the radio with value "courant"
$radios.val( [ "fin", "courant" ] ); // not useful in radio button context, but you can use this for checkboxes

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM