3

getElementById を使用する代わりに getElementByValue を使用したいオプション選択を検出するのに問題があります。存在しますか?? これは私のコードです

     <script type="text/javascript">
     var total;


function oneway(){

document.getElementById("return_date").style.visibility = "hidden";

document.getElementById("return_time").style.visibility = "hidden";

}

function return_way(){

document.getElementById("return_date").style.visibility = "visible";

document.getElementById("return_time").style.visibility = "visible";

}

function state_price(){
var way=document.getElementById("direction").value;

var placefrom = document.getElementById("state_from").value;
var placeto   = document.getElementById("state_to").value;

if (direction == "oneway"){
    if ((placefrom=="Batu Pahat" && placeto=="Alor Setar") ||(placefrom =="Alor Setar" && placeto=="Batu Pahat")){

        total=55.00;
        document.booking.total_price.value = total;


    }else if ((placefrom=="Batu Pahat" && placeto=="Bachok")||(placefrom=="Bachok" && placeto=="Batu Pahat"))
    {

        total=60.00;
        document.booking.total_price.value = total; 


    }else if ((placefrom=="Batu Pahat" && placeto=="A Famosa") || (placefrom=="A Famosa" && placeto=="Batu Pahat"))
    {

        total=65.50;
        document.booking.total_price.value = total;

    }else if ((placefrom=="Batu Pahat" && placeto=="Bahau")||(placefrom=="Bahau" && placeto=="Batu Pahat"))
    {
        total=70.00;
        document.booking.total_price.value = total;

    }else if ((placefrom=="Batu Pahat" && placeto=="Bentong") ||(placefrom=="Bentong" && placeto=="Batu Pahat"))
    {
        total=75.50;
        document.booking.total_price.value = total;
    }else if ((placefrom=="Batu Pahat" && placeto=="Kubang Kerian") ||(placefrom=="Kubang Kerian" && placeto=="Batu Pahat"))
    {
        total=80.00;
        document.booking.total_price.value = total;

    }

    else
    {

        document.booking.txttotal.value = "Fail";
        window.alert("Please enter a different destination");

    }
}
else
{



///something else

}
}
</script>

これは私のHTMLコーディングの一部です

 <tr > 
 <td>Travel Direction:</td>
 <td >

 <input type="radio" name ="direction" value = "oneway"  onclick = "oneway()"/>One Way
 <input type="radio" name ="direction" value = "return"  onclick = "return_way()"/>Return

 </td>
 </tr>

PHPコーディングで使用しているので、名前を使用したくないので、使用したくありません.上記のようにjavascriptを実行できる方法はありますか(getElementByValue)?ありがとうあらかじめ

4

4 に答える 4

7

getElementByValue を使用したいです。存在しますか??

いいえ。独自に記述することもできます (ドキュメント内のすべての要素をループしてテストする) か、現在の値ではなく初期値に関心がある場合は、属性セレクターでquerySelector/querySelectorAllを使用できます。

通常の状況では値を変更しない (チェックされた状態のみ) ラジオ ボタンを使用しているため、正常に動作するはずです。

document.querySelector('input[value=oneway]');
于 2013-05-13T16:40:31.013 に答える
1

id 属性を設定できない場合は、CSS クラスを適用して使用できます。

document.getElementsByClassName

document.getElementsByClassName (IE 9.0 以降が必要)

または、値属性をチェックする DOM 全体をトラバースすることもできます。しかし、これは非常に遅く、jQuery などを使用しないと非常にバグが発生しやすくなります。

編集: Quentin の回答の方が優れています。document.querySelector は IE8 以降で動作し、CSS クラスを作成する必要はありません。

于 2013-05-13T16:41:03.193 に答える
1

ここでの問題の 1 つは、getElementById を使用していて、名前を使用していることです。これは機能しません。

個人的には、最初のラジオ ボタン [一方向] がオンになっているかどうかを確認します。

var isOneWay = document.forms[0].direction[0].checked;

if ステートメントよりも、ブール値を使用してロジックを決定できます。

于 2013-05-13T16:39:58.347 に答える
-2

値セレクターがある jQuery を使用します。例: $('input["value=your-value"]')

于 2013-05-13T16:41:28.800 に答える