たとえば、オプション選択ボックスで 70 歳以上が選択されたときに「退職」を自動的に入力する、自動生成されたフィールドに入力しようとしています。
私はそれで何か助けを得ることができます:)
たとえば、オプション選択ボックスで 70 歳以上が選択されたときに「退職」を自動的に入力する、自動生成されたフィールドに入力しようとしています。
私はそれで何か助けを得ることができます:)
考えられる方法の 1 つを次に示します。
var age = document.getElementById('age');
var status = document.getElementById('status');
age.onchange = function(){
switch(parseInt(this.value)){
case 20: status.value = 'working'; break;
case 50: status.value = 'working'; break;
case 70: status.value = 'retired'; break;
default: status.value = 'Unknown';
}
};
新しいフィールドを「生成」する代わりに、既存のフィールドの可視性を条件付きで変更することができます。最も簡単な例:
<script>
function checkAge(f){
var age = f.options[f.selectedIndex].value
var field = document.getElementById('field');
if(age>69)
field.style.display='';
else
field.style.display='none';
}
</script>
<select onchange="checkAge(this)">
<option value="10">10 years</option>
<option value="30">30 years</option>
.
.
.
<option value="70">70 years</option>
</select>
<input type="text" id="field" value="" style="display:none" />
最良の方法は、フィールドで div を表示/非表示にすることです。さらに、リソースの消費が少なくなります...それ以外の場合は、次のことを試してください。
<form id="my-form">
<input id="age-textbox" name="age" text="" />
</form>
$('#age-textbox').blur(function(){
if (!isNaN($(this).val())
&& parseInt($(this).val()) >= 70)
{
var field = $('<input/>', { id: "test", name: "test" });
$('#my-form').append(field);
}
});