javascript函数无法在表单中工作

时间:2017-11-05 03:31:54

标签: javascript html jsp

希望你们都好。我有一个问题我的javascript函数没有被调用我不知道为什么 我很困惑,为什么函数没有调用我在我的另一种形式使用了同样的流程,这也工作正常,但在这里它是一团糟 Kinldy看看代码并帮助我

由于

的javascript:

function checkPriceValidation(){
    var flight=document.getElementById('flightno').value;
    var type=document.getElementById('type').value;
    var price=document.getElementById('price').value;

    if(flight==="volvo"){
        alert("Please select flight number !!");
        return false;
    }
    else if(type==="default"){
        alert("Please select type!!");
        return false;
    }
    else if(price===""){
        alert("Please select the price !!");
        return false;
    }
    return false;
}

html:

<form 
    class="" 
    method="post" 
    onsubmit="return javascript:checkPriceValidation();"
    action="<%=response.encodeURL(request.getContextPath()+"/FillList")%>"
>
    <div style="color:red;font-size:18px ">${error}</div>
    <% session.setAttribute("error", "");%>
    <table>
        <tr>
            <td>
                <div class="form-control" style="width: auto">
                    <select style="color: black" name="flightno" >
                        <option value="volvo">Select Flight</option>
                        <%
                            ArrayList<FlightClass> list;
                            list = (ArrayList<FlightClass>) request.getSession().getAttribute("list");
                            for (FlightClass post : list) {
                        %>
                            <option><%=post.getID()%></option>
                        <%  }  %>
                    </select>
                </div>
            </td>
        </tr>
        <tr>
            <td>
                <div class="form-control" style="width: auto">
                    <select style="color: black" name="type">
                        <option value="default">Select Type</option>
                        <option>Economy</option>
                        <option>Business</option>
                        <option>First Class</option>
                    </select>
                </div>
            </td>
            <td>
                <input 
                    type="number"
                    min="0" 
                    step="1"
                    class="form-control"
                    name="price" 
                    id="price"
                    placeholder="Price"
                />
            </td>
        </tr>
    </table>
    <br/>
    <button input-type="submit" value="Submit" class="btn btn-lg btn-primary" >SET</button> 
</form>

1 个答案:

答案 0 :(得分:1)

onsubmit属性中,您应该只使用return checkPriceValidation()。试试下面的代码,看看差异

&#13;
&#13;
function submitThis(){
  alert('Hi there');
  return false;
}
&#13;
<form onsubmit="return javascript:submitThis()">
  <button>This form will be submitted</button>
</form>

<form onsubmit="return submitThis()">
  <button>This form will not be submitted</button>
</form>
&#13;
&#13;
&#13;