dポイントプレゼントキャンペーン実施中!

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>

A 回答 (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');" />
    • good
    • 0
この回答へのお礼

ありがとうございます。
回答1,回答2のやりかたで、できました。
これで、やりたいことができそうです。
本当に助かりました。

お礼日時:2009/11/10 09:50

お探しのQ&Aが見つからない時は、教えて!gooで質問しましょう!