下記のようなコードでイベントを発生したいと考えています。
キーボードによる打ち込みではなく、文章をコピー&ペーストすることで
「firstBox2」の表示を消し、「secondBox2」を表示させたいと考えています。
ご教授お願いします。
<input id="target_input" type="text" value="" />
<select id="firstBox2">
<option value="select1">-----</option>
<option value="select2">------</option>
</select>
<select id="secondBox2" style="display:none;">
<option value="select1">午前中</option>
<option value="select2">2度目以降の利用</option>
</select>
<script type="text/javascript">
var timer = null;
var input = document.getElementById("target_input");
var prev_val = input.value;
input.addEventListener("focus", function(){
window.clearInterval(timer);
timer = window.setInterval(function(){
var new_val = input.value;
if(prev_val != new_val){
document.getElementById('firstBox2').style.display = "none";
document.getElementById('secondBox2').style.display = "";
};
prev_value = new_value;
}, 10);
}, false);
input.addEventListener("blur", function(){
window.clearInterval(timer);
}, false);
}
</script>
A 回答 (2件)
- 最新から表示
- 回答順に表示
No.2
- 回答日時:
xxx.style.display = ""; //?
<style>
xxx { display:; } /* ? */
</style>
--
input.value = '0';
if (input.value) {
;// ?
}
No.1
- 回答日時:
よくわかりませんが、とりあえずご提示の内容で…
・スクリプトの最後の「}」が余分
・prev_value = new_value; の行で、未定義変数参照のエラーになっている
を修正すれば動作すると思われます。
ついでに、余計なお世話ですが…
全体でやっていることは、selectの表示切替えを1回だけ行っているというもので、それでよいのであれば、最初の値の変化を取得できさえすれば良いと考えられます。
・focusやblurで処理する必要はなく、selectを切り替えたらsetIntervalを止めればよい
・prev_valやnew_valで値を控えているが、初期値と変わったことを判定条件にすれば
if(input.value){~~}
のような条件でも十分です。(値を控えておく必要がなくなる)
・控えがいらないので、現在エラーとなっている、prev_value = new_value;なども不要
あと、setIntervalのintervalはもう少し時間をとっても良いかも…
てなことを反映するとこんな感じでも、ご提示の処理は可能かと…
var input = document.getElementById("target_input");
var timer = setInterval(function(){
if(input.value){
clearInterval(timer);
document.getElementById('firstBox2').style.display = "none";
document.getElementById('secondBox2').style.display = "";
};
}, 50);
お探しのQ&Aが見つからない時は、教えて!gooで質問しましょう!
関連するカテゴリからQ&Aを探す
おすすめ情報
- ・漫画をレンタルでお得に読める!
- ・街中で見かけて「グッときた人」の思い出
- ・「一気に最後まで読んだ」本、教えて下さい!
- ・幼稚園時代「何組」でしたか?
- ・激凹みから立ち直る方法
- ・1つだけ過去を変えられるとしたら?
- ・【あるあるbot連動企画】あるあるbotに投稿したけど採用されなかったあるある募集
- ・【あるあるbot連動企画】フォロワー20万人のアカウントであなたのあるあるを披露してみませんか?
- ・映画のエンドロール観る派?観ない派?
- ・海外旅行から帰ってきたら、まず何を食べる?
- ・誕生日にもらった意外なもの
- ・天使と悪魔選手権
- ・ちょっと先の未来クイズ第2問
- ・【大喜利】【投稿~9/7】 ロボットの住む世界で流行ってる罰ゲームとは?
- ・推しミネラルウォーターはありますか?
- ・都道府県穴埋めゲーム
- ・この人頭いいなと思ったエピソード
- ・準・究極の選択
- ・ゆるやかでぃべーと タイムマシンを破壊すべきか。
- ・歩いた自慢大会
- ・許せない心理テスト
- ・字面がカッコいい英単語
- ・これ何て呼びますか Part2
- ・人生で一番思い出に残ってる靴
- ・ゆるやかでぃべーと すべての高校生はアルバイトをするべきだ。
- ・初めて自分の家と他人の家が違う、と意識した時
- ・単二電池
- ・チョコミントアイス
デイリーランキングこのカテゴリの人気デイリーQ&Aランキング
-
javascriptでhiddenに二次元配...
-
value内に変数を入れたい
-
eval()を使わずに数値を取得し...
-
UART通信の取説で,left floati...
-
セレクトボックスの初期選択状...
-
クイズ作成:15個の問題から5個...
-
hiddenのvalueの値を変えたい
-
数値の掛け算の不具合について
-
SELECTの選択状態を、複数のVAL...
-
フォーム名に指定文字列が含ま...
-
ソースプログラムで変数の名前...
-
ラジオボタンは0からと1からの...
-
ログイン、パスワードをセット...
-
C言語クイックソートの比較総回...
-
プルダウンから移動時のフレー...
-
ラジオボタン、チェックボック...
-
ループで連続したフォームの値...
-
複数のsubmitボタンで押された...
-
javascriptにてHTMLのhiddenエ...
-
return trueとreturn falseの用...
マンスリーランキングこのカテゴリの人気マンスリーQ&Aランキング
-
value内に変数を入れたい
-
UART通信の取説で,left floati...
-
javascriptでhiddenに二次元配...
-
hiddenのvalueの値を変えたい
-
VB.NET DateTimeの型について
-
テキストボックスに入力された...
-
CDOMailで本文テキストの文字コ...
-
setIntervalの間隔を途中で変更...
-
javascriptにてHTMLのhiddenエ...
-
ラジオボタンの選択で解答・点...
-
子から親へチェックボックスの...
-
フォームで入力した値を別のフ...
-
複数のsubmitボタンで押された...
-
selectboxのoptionタグのvalue...
-
Pythonで会員サイトの自動ログ...
-
クイズ作成:15個の問題から5個...
-
どのボタンがクリックされたの...
-
eval()を使わずに数値を取得し...
-
VBAをJavaScriptに変換したいです
-
ホームページビルダーのスクリ...
おすすめ情報