Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
765 views
in Technique[技术] by (71.8m points)

html - javascript function not working in form

Hope you all are fine . I have a problem my javascript function is not being called i dont know why I am very confused why the function is not calling I used this same flow in my another form too that was also working fine but here Its a mess Kinldy have a look on code and help me

Thanks

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>
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

In your onsubmit attribute, you should just use return checkPriceValidation(). Try out the code below and see the difference

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>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...