希望大家一切都好。我有一个问题,我的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>

最佳答案

onsubmit属性中,应该只使用return checkPriceValidation()。试试下面的代码,看看有什么区别



function submitThis(){
  alert('Hi there');
  return false;
}

<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>

10-05 21:07
查看更多