English 中文(简体)
给予依赖的下游名单选择创造功能的价值
原标题:Giving a dependent drop-down list option a value to create a function
  • 时间:2012-01-12 12:44:32
  •  标签:
  • javascript

我对贾瓦特来说非常新(头几天)。

创建航空公司预订表,希望显示一个座位价格,取决于乘客选择的等级。

不要确定如何从受扶养的下级文字中收回价值,从而可以形成一个阵列,将选定的价值与价格挂钩。 我的描述......

<script language="javascript" type="text/javascript">

function dropdownlist(listindex)
{
    document.flightform.pitch.options.length = 0;
    switch (listindex)
    {
        case "Economy":
            document.flightform.pitch.options[0]=new Option("28","28");
            document.flightform.pitch.options[1]=new Option("29","29");
            break;
        case "Biz":
            document.flightform.pitch.options[0]=new Option("30","");
            document.flightform.pitch.options[1]=new Option("32","");
            break;
        case "First":
            document.flightform.pitch.options[0]=new Option("40","");
            break;
    }
    return true;
}
var Seat_pitch= new Array ();

Seat_pitch["28"] = 400;
//etc.....
//etc.....

function getSeatpitch()
{
    var Seatpitch=0;
    var theForm = document.forms["flightform"];
    var selectedpitch = theForm.elements["pitch"];

    Seatpitch = Seat_pitch[selectedpitch.value];

    return Seatpitch;
}

function calcTotal()
{
    var Total = getSeatpitch()  ;

    //display the result
    var divobj = document.getElementById( totalPrice );
    divobj.style.display= block ;
    divobj.innerHTML = "Your flight Total is &pound;"+Total;
}

</script>

<htmL>
<body>
    <label>Class type</label><br />
    <select id="class"  name= class  onchange="javascript: dropdownlist(this.options[this.selectedIndex].value);">

        <option value="Economy">Economy</option>
        <option value="Biz">Business</option>
        <option value="First">First</option>
    </select>
    <br/><br/>
    <script type="text/javascript" language="JavaScript">
        document.write( <select name="pitch" id="pitch"></select> )
    </script>
    <div id="totalPrice"></div>

</html>
</body>
问题回答

你没有在你本人的“飞行”形式,但你在你的 j中再次提到这一点。

事实上,贵方是一片bit子,重新排列如下:

<html>
<script>
...
</script>
<body>
<form name="flightform">
...
</form>
</body>
</html>

You should then be able to get the price by calling the getSeatpitch function.

似乎你没有在你的代码中打电话到calcTotal

You could just call it in the onchange event of the <select name="pitch">, like

<script type="text/javascript" language="JavaScript">
    document.write( <select name="pitch" id="pitch" onchange="calcTotal();"></select> )
</script>

See it in this fiddle

I don t understand why is this <select> being written by means of document.write, though.

页: 1 活动听众,这样,在改变这一类别(因此是itch)时,总会更新。 另请打电话dropdownlist()和calcTotal(>),请上页时间(例如,有<上载人=“calcTotal()”>),因为你将先选择第一个数值。

甚至更糟的是,为退学提供了空白值,并在<条码>上对其进行了核对,以明确显示没有选定班级的全文。

另外,关于如何从<条码>和代号;中获取价值这一原始问题,你只能使用<条码>价值的<<<>> >条码/条码>,而不是<条码> 权标[部分]。

<><>>>>>> 我立即更新了提供空白选择的金字塔,并在没有选择适当选择的情况下,清除显示总数。

你们是否想利用 j框架? 有许多有益的东西,因此,你不需要自行自行自行车。 例如,可以改写成:

<html>
    <head>
        <!-- Connect jQuery from Google library storage -->
        <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js" type="text/javascript"></script>
    </head>
    <body>
        <!-- Create select for class, with id #class -->
        <select id="class" name="class">
            <option value="0">Econom</option>
            <option value="1">Business</option>
            <option value="2">First</option>
        </select>
        <!-- Create select for place, with id #place -->
        <select id="place" name="place" disabled="disabled"></select>
        <!-- Create view place for price, with id #price -->
        <span id="price"></span>
        <script type="text/javascript">
            // available places to choose 
            var available = {
                0:      {
                    25: 50
                },
                1:      {
                    26: 100
                },
                2:      {
                    21: 200,
                },
            }

            // When the document is totally loaded, do that things that s defined in function(e) {}
            $(document).ready(function(e){
                // Bind trigger for class select changing, where $( select#class ) - find element that in DOM select inputs and has id="class"
                // When it change, call function(e) {}, inside this function $(this) will be the select itself
                $( select#class ).live( change , function(e){
                    // find place select input
                    var place = $( select#place );
                    // if it s disabled, unblock it, and clean all options inside
                    place.removeAttr( disabled ).find( option ).remove();
                    // foreach places in available create option and put it into place select
                    for (var i in available[$(this).val()]) {
                        place.append($( <option /> ).val(i).html(i));
                    }
                    // behave like place select have changed, and trigger listener above
                    place.change();
                });

                $( select#place ).live( change , function(e){
                    $( span#price ).html(available[$( select#class ).val()][$(this).val()]);
                });
            });
        </script>
    </body>
</html>

Totally working example. Somehow like that... Strongly recommend you not to use raw JavaScript.





相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.

热门标签