私は、とりわけ、気候パラメータを選択するための2つのフィールドを含むdjangoフォームを持っています。これらのフィールドは両方とも同じCHOICESによって入力されますが、最初に選択されたものに影響されるようにscondフィールドが必要でした。基本的に、最初にパラメータが選択された場合、2番目の選択肢からパラメータを削除する必要があります。
私は今、1つの問題を除いてこれに非常にうまく機能するコードを持っています。フィールドは言語に対応している必要があります。
forms.py
class ClimateForm(forms.Form):
...
param1 = forms.ChoiceField(choices=PARAM_CHOICES, label=_('Y axis one'), initial=_('Not Specified'),
widget=forms.Select(attrs={"onChange":'activateParam()'}),
)
param2 = forms.ChoiceField(choices=PARAM_CHOICES, label=_('Y axis two'), initial=_('Not Specified'),
required = False,
)
...
activateParam()
次に、これを行う関数を呼び出します。
filter_form.js:
function activateParam() {
// get value to exclude from select
var param1Val = document.getElementById("id_param1").value;
// get next element id
var param2 = document.getElementById("id_param2");
// param2.setAttribute("style", "display:block");
updateSelect($('select[name=param2]'), param1Val);
}
function updateSelect(select, value) {
// find and remove existing options
select.find('option').remove();
// loop through results and append to select as options
for (var k in climate_params) {
if (k!=value) {
select.append($('<option value="'+k+'">'+climate_params[k]+'</option>'));
}
}
}
climate_params
2番目のフィールドに入力する値と文字列の配列です。
var climate_params = {
'': 'Not Specified',
'mean_air_temp': 'Mean Air Temperature',
'min_air_temp': 'Min Air Temperature',
'max_air_temp': 'Max Air Temperature',
'sea_temp': 'Sea Surface Temperature',
'mean_rel_hum': 'Mean Relative Humidity',
'precipitation': 'Preciptitation',
};
したがって、この配列を変更したり、別の配列を含めたりすることはできますが、言語状態をこのJSスクリプトに渡す方法を知る必要があります。JSで言語変数に直接アクセスする方法はありますか?
どんな助けでも大歓迎です。