プロが教えるわが家の防犯対策術!

下記のソースのように「word」というテキストフィールドの
入力チェック後ボタンのvalueをword.phpに渡したいのですがどのようにしたらいいのでしょうか?
入力チェックをせず
input type="button"ではなくinput type="submit" にするとword.phpに渡せるのですが・・・
ようはA、B、Cそれぞれのボタンを押した時で
word.phpでbtnのvalueをPOSTで受け取り
処理をvalue A、B、Cで分岐させたいんです。
よろしくお願いします。

<html>
<head>
<SCRIPT language="JavaScript">
<!--
function chk(chkfrm){
var tmp = chkfrm.word.value.length;
if(tmp > 50 ){
alert("文字数が多すぎます");
}
else{
chkfrm.submit();
}
}
//-->
</SCRIPT>
</head>
<body>

<form name="form" method="post" action="word.php">
<input name="word" type="text" id="word">
<input type="button" name="btn" value="A" onclick="chk(this.form);">
<input type="button" name="btn" value="B" onclick="chk(this.form);">
<input type="button" name="btn" value="C" onclick="chk(this.form);">
</form>

</body>
</html>

A 回答 (2件)

ごめんなさい、1をちょっと訂正させてください。


form.btn.value = chk.value;

chkfrm.btn.value = chk.value;
としてください。
(もとのままでも動作はするのですが。汎用的ではないので^^;)
    • good
    • 0
この回答へのお礼

そこまで考えてもらってありがとうございます!
助かりました。

お礼日時:2007/09/04 19:08

<html>


<head>
<SCRIPT language="JavaScript">
<!--
function chk(chk){
var chkfrm = chk.form;
var tmp = chkfrm.word.value.length;
if(tmp > 50 ){
alert("文字数が多すぎます");
}
else{
form.btn.value = chk.value;
chkfrm.submit();
}
}
//-->
</SCRIPT>
</head>
<body>

<form name="form" method="get" action="sub.htm">
<input name="word" type="text" id="word">
<input type="button" value="A" onclick="chk(this);">
<input type="button" value="B" onclick="chk(this);">
<input type="button" value="C" onclick="chk(this);">
<input type="hidden" name="btn"/>
</form>

</body>
</html>

こんな感じでどうでしょう?
    • good
    • 0
この回答へのお礼

めちゃくちゃ早い回答ありがとうございますm(__)m
ばっちりです!お手数おかけしました。

お礼日時:2007/09/04 19:03

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