2016-03-27 26 views
3

私はオプションの値に従って入力タイプを変更しようとしています。オプション値に応じて入力タイプを変更するにはどうすればよいですか?

日付オプションを選択すると、入力フィールドのタイプを日付タイプに変更する必要があります。

function func2() { 
 
    if (document.getElementById('field_search').value=="khao_date") { 
 
    document.getElementById('input_search').type == 'date'; 
 
    } 
 
    else { 
 
    document.getElementById('input_search').type == 'text'; 
 
    } 
 
}
<select id="field_search" name="field_search" size=1 onchange="func2()"> 
 
    <option value="khao_date">Date</option> 
 
    <option value="khao_detail">Detail</option> 
 
</select> 
 

 
<input id="input_search" type="text">

何とかそれは変更されません。誰か助けてくれますか? ありがとうございます!

答えて

2

代わりにsetAttribute('type', '...')を使用してください。 Chromeでテストした。..

function func2() { 
 
    if (document.getElementById('field_search').value=="khao_date") { 
 
    document.getElementById('input_search').setAttribute('type', 'date'); 
 
    } 
 
    else { 
 
    document.getElementById('input_search').setAttribute('type', 'text'); 
 
    } 
 
}
<select id="field_search" name="field_search" size=1 onchange="func2()"> 
 
    <option value="khao_date">Date</option> 
 
    <option value="khao_detail">Detail</option> 
 
</select> 
 

 
<input id="input_search" type="text">

+0

それは働きました。ありがとうございました! –

関連する問題