如何在表单按钮中使用javascript函数?

时间:2013-10-07 05:41:10

标签: javascript html

我对html一无所知,尤其是javascript ..但是我们有关于你如何调用位于html标签的<head></head>的javascript函数的活动?

如果有几个功能怎么办?我可以在一个按钮上同时拨打电话吗?或者我应该创建另一个函数并将所有函数放在那里?

这就是我们的活动所有......在一个javascript按钮中,点击它时,它必须计算所有交易?我有5个函数,其中一个由按钮标记调用,而其他4个函数在函数内部。我真的不知道该怎么做......但是当我点击按钮时,什么都不会发生。顺便说一句,它是一个预订表格,所以当点击按钮时,它必须计算所有输入并显示一个确认页面/警报与价格等。谢谢你们!

这是我的表格代码:

<form name="reserve" action="" id="reserve" method="post">

<fieldset>
<legend>Contact Information</legend>
<label for="name">Name: </label>
<input type="text" name="firstname" value="firstname" 
  onfocus="if (this.value==this.defaultValue)this.value='';"
  onblur="if(this.value=='')this.value=this.defaultValue;"/>
<input type="text" name="lastname" value="lastname"
  onfocus="if(this.value==this.defaultValue)this.value='';" 
  onblur="if(this.value=='')this.value=this.defaultValue;"/>
<br>

<label for="address">Address: </label>
<textarea name="address" cols="30" rows="3"></textarea>

<br>

<label for="city">City: </label>
<input type="text" name="city">

<label for="country">Country: </label>
<select name="country">
<option value=""></option>
<option value="PH">Philippines</option>
<option value="TH">Thailand</option>
<option value="VN">Vietnam</option>
<option value="MY">Malaysia</option>
<option value="ID">Indonesia</option>
<option value="SG">Singapore</option>
</select>

<br>

<label for="email">Email: </label>
<input type="email" name="email">

<label for="phone">Phone: </label>
<input type="tel" name="phone">
</fieldset>
<hr>

<fieldset>
<legend>Accomodation Request</legend>
<label for="checkin">Check-in: </label>
<input type="date" name="checkin">

<label for="checkout">Check-out: </label>
<input type="date" name="checkout">

<br>

<label for="roomtype">Room type: </label> <br>
<input type="checkbox" id="s" name="roomtype" value="superior">Superior  |||||
<label for="sguest">No.of guests: </label>
<input type="text" id="supg" name="sguest" size="3"> <br>

<input type="checkbox" id="d" name="roomtype" value=deluxe">Deluxe |||||||
<label for="dguest">No.of guests: </label>
<input type="text" id="delg" name="dguest" size="3"> <br> 

<input type="checkbox" id="p" name="roomtype" value="Premier">Premier |||||
<label for="pguest">No.of guests: </label>
<input type="text" id="premg" name="pguest" size="3"> <br>
</fieldset>

<br>
<hr>

<label for="adinfo">Additional Information:</label>
<textarea name="adinfo" cols="40" rows="10"></textarea>
<br><br>
<hr>
<input type="button" name="submit" onclick="formSubmit()" 
  class="submit" value="Reserve">



</form>

这是javascript代码:

function superiorroom(){
    var roomprice=0;
    var theForm = document.forms["reserve"]
    var s = theForm.elements["s"]
    var supg = theForm.elements["supg"]
    var t=0;

    If (s.checked==true)
    {
        roomprice=5400;
        squantity=parseInt(squantity.value);
        t=parseInt(t);
        t= (roomprice*squantity)*supg;
    }
    return t;
}

function deluxeroom(){
    var roomprice=0;
    var theForm = document.forms["reserve"]
    var d = theForm.elements["d"]
    var delg = theForm.elements["delg"]
    var u=0;

    If (d.checked==true)
    {
        roomprice=7200;
        dquantity=parseInt(dquantity.value);
        u=parseInt(u);
        u= (roomprice*dquantity)*delg;
    }
    return u;
}

function premiumroom(){
    var roomprice=0;
    var theForm = document.forms["reserve"]
    var p = theForm.elements["p"]
    var premg = theForm.elements["premg"]
    var v=0;

    If (p.checked==true)
    {
        roomprice=9800;
        pquantity=parseInt(pquantity.value);
        v=parseInt(v);
        v= (roomprice*pquantity)*premg;
    }
    return u;
}


</script>

我希望你能帮助我们!

2 个答案:

答案 0 :(得分:0)

您好我已经更改了您的代码一点检查

<input type="button" name="submit" onclick="return formSubmit()" class="submit" value="Reserve">

答案 1 :(得分:0)

我改变了你的代码:试试这个 按钮

<input type="button" id="submit" name="submit" onclick="return formSubmit()" class="submit" value="Reserve">

Scripts

<script>
$("#submit").click(function () {
       if ( document.getElementById("s").checked =checked)
           superiorroom();
        else if(document.getElementById("d").checked =checked)
            deluxeroom();
        else if(document.getElementById("p").checked =checked)
           premiumroom();

    });
</script>

<script>
function superiorroom(){
    var roomprice=0;
    var theForm = document.forms["reserve"]
    var s = theForm.elements["s"]
    var supg = theForm.elements["supg"]
    var t=0;

    If (s.checked==true)
    {
        roomprice=5400;
        squantity=parseInt(squantity.value);
        t=parseInt(t);
        t= (roomprice*squantity)*supg;
    }
    return t;
}

function deluxeroom(){
    var roomprice=0;
    var theForm = document.forms["reserve"]
    var d = theForm.elements["d"]
    var delg = theForm.elements["delg"]
    var u=0;

    If (d.checked==true)
    {
        roomprice=7200;
        dquantity=parseInt(dquantity.value);
        u=parseInt(u);
        u= (roomprice*dquantity)*delg;
    }
    return u;
}

function premiumroom(){
    var roomprice=0;
    var theForm = document.forms["reserve"]
    var p = theForm.elements["p"]
    var premg = theForm.elements["premg"]
    var v=0;

    If (p.checked==true)
    {
        roomprice=9800;
        pquantity=parseInt(pquantity.value);
        v=parseInt(v);
        v= (roomprice*pquantity)*premg;
    }
    return u;
}
</script>