HTMLの制御にjQueryを使用しています。
functionに引数としてIDを渡して処理をしたいのですが、やりかたが分かりません。
テスト的にtest1Script、test2Scriptを作成しました。
test1と同様の結果を、引数にIDを渡すtest2Scriptでも得たいのですが、
失敗(1)や失敗(2)のように書いた場合、うまくいきません。何か良い方法はないでしょうか。ご教授下さい。
<script type="text/javascript">
function test1Script(){
alert($("#textfield1").val());
}
function test2Script(objId){
//alert($("#objId").val()); 失敗(1)
//alert($(#objId).val()); 失敗(2)
}
</script>
</head>
<body>
<input type="text" id="textfield1" value="test1" />
<input type="button" id="button1" value="テスト1" onclick="test1Script();" />
<input type="button" id="button2" value="テスト2" onclick="test2Script('textfield1');" />
</body>
</html>
No.1ベストアンサー
- 回答日時:
回答1
function test2Script(objId){
alert($(objId).val());
}
<input type="button" id="button2" value="テスト2" onclick="test2Script('#textfield1');" />
回答2
function test2Script(objId){
alert($("#" + objId).val());
}
<input type="button" id="button2" value="テスト2" onclick="test2Script('textfield1');" />
お探しのQ&Aが見つからない時は、教えて!gooで質問しましょう!
関連するカテゴリからQ&Aを探す
おすすめ情報
- ・漫画をレンタルでお得に読める!
- ・人生のプチ美学を教えてください!!
- ・10秒目をつむったら…
- ・あなたの習慣について教えてください!!
- ・牛、豚、鶏、どれか一つ食べられなくなるとしたら?
- ・【大喜利】【投稿~9/18】 おとぎ話『桃太郎』の知られざるエピソード
- ・街中で見かけて「グッときた人」の思い出
- ・「一気に最後まで読んだ」本、教えて下さい!
- ・幼稚園時代「何組」でしたか?
- ・激凹みから立ち直る方法
- ・1つだけ過去を変えられるとしたら?
- ・【あるあるbot連動企画】あるあるbotに投稿したけど採用されなかったあるある募集
- ・【あるあるbot連動企画】フォロワー20万人のアカウントであなたのあるあるを披露してみませんか?
- ・映画のエンドロール観る派?観ない派?
- ・海外旅行から帰ってきたら、まず何を食べる?
- ・誕生日にもらった意外なもの
- ・天使と悪魔選手権
- ・ちょっと先の未来クイズ第2問
- ・【大喜利】【投稿~9/7】 ロボットの住む世界で流行ってる罰ゲームとは?
- ・推しミネラルウォーターはありますか?
- ・都道府県穴埋めゲーム
- ・この人頭いいなと思ったエピソード
- ・準・究極の選択
デイリーランキングこのカテゴリの人気デイリーQ&Aランキング
-
変数の宣言?
-
クリックさせたいが、click()が...
-
onclickが動作しない
-
クリックでテーブル内の背景色...
-
小窓が残像を残して移動
-
ラジオボタンの選択で解答・点...
-
tableの任意行にfocusをあてる
-
クリックされた罫表セルの行番...
-
全てのselect要素をデフォルト...
-
プルダウン 項目が多いので先頭...
-
一覧から選択した行の行番号を...
-
submitするとなぜか2度実行する
-
自動的に連番生成したURLにリン...
-
ドロップダウンリストの値の足...
-
チェックボックス付きのテーブ...
-
チェックボックスにチェックが...
-
複数のプルダウンメニューの表...
-
リンク先にまで色変更されたま...
-
セレクトボタンで特定の項目で...
-
新規ウインドウから他のページ...
マンスリーランキングこのカテゴリの人気マンスリーQ&Aランキング
-
onchangeイベントを強制的に発...
-
javaScriptの変数をJavaの変数...
-
クリックさせたいが、click()が...
-
onclickが動作しない
-
formのfileの値をhiddenでも持...
-
innerHTML内では改行は禁止?
-
追加ボタンを押した際に ok ボ...
-
ボタンかリンクをクリックする...
-
JQueryでfunctionに引数としてI...
-
JavaScriptのfileオブジェクト...
-
JSのボタンを複数う使うには
-
クリックでテーブル内の背景色...
-
VB.NETで<Input>タグ、<text...
-
ブラウザの外にあるマウスの情...
-
jQueryでshow/hideが上手く行か...
-
onClickがinput type="image"だ...
-
【javascript】ボタンクリック...
-
開いた子ウィンドウにあるボタ...
-
javascriptで作成されたテーブ...
-
jQueryで設定したイベントハン...
おすすめ情報