2012-12-19 39 views
7

Trong trang htm của tôi, tôi muốn sử dụng nút gửi biểu mẫu của tôi bằng cách sử dụng hàm. Tôi sử dụng xác nhận trên các lĩnh vực của tôi và khi tôi nhấn nút gửi nó sẽ hiển thị thông báo lỗi nhưng doesnot giữ lại giá trị cuối cùng nó làm mới hình thức của tôi. Tôi chỉ muốn rằng các giá trị mà tôi đã nhập sẽ tồn tại ở đó và chỉ trường có thông báo lỗi sẽ biến mất. tất cả các lĩnh vực khác vẫn còn đó. Đây là mã của tôi:không muốn làm mới và giữ lại các giá trị được chèn cuối cùng

 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
    <html> 
    <head> 
    <!--<meta http-equiv="refresh" content="text/html; charset=iso-8859-1" />--> 
    <!-- 
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/> 
    --> 
    <title>Phone Registration</title> 
    <link rel="stylesheet" type="text/css" href="file:///C|/xampp/htdocs/track/css/view.css" media="all"/> 
    <script type="text/javascript" src="file:///C|/xampp/htdocs/track/javascript/view.js"></script> 
    <script type="text/javascript" src="file:///C|/xampp/htdocs/track/javascript/external.js"></script> 
    <link rel="stylesheet" href="http://code.jquery.com/ui/1.9.2/themes/base/jquery-ui.css" /> 
    <script src="http://code.jquery.com/jquery-1.8.3.js"></script> 
    <script src="http://code.jquery.com/ui/1.9.2/jquery-ui.js"></script> 

    <script> 
     $(function() { 
      $("#datepicker").datepicker(); 
     }); 
     </script> 

    </head> 
    <body id="main_body" > 
     <img id="top" src="file:///C|/xampp/htdocs/track/images/top.png" alt=""/> 
      <div id="form_container"> 
      <h3> 
       <a href="file:///C|/xampp/htdocs/track/phonereg.php">Phone Registration</a> 
       <a href="file:///C|/xampp/htdocs/track/officerreg.php">Officer Registration</a> 
       <a href="file:///C|/xampp/htdocs/track/customerreg.php">Customer Registration</a> 
       <a href="file:///C|/xampp/htdocs/track/taskentry.php">Task Entry</a> 
      </h3>  
      <form name="order" id="form_532698" class="appnitro" method="post" action="" > 
       <div class="form_description"> 
       <h2>Phone Registration</h2> 
      </div>      
      <ul > 
       <li> 
       <span> 
       <label class="description">IMEI # : </label> 
       <input name="imei" id="imeinum" class="element text medium" type="text" maxlength="15" value=""/> 

       </span> 
       </li> 
       <li> 
       <span> 
       <label class="description" >Phone Number </label> 
       <input name="phone" id="phnum" class="element text medium" type="text" maxlength="11" value="" onblur="validate(this)"/> 

       </span> 
       </li> 
       <li> 
       <span> 
       <label class="description" >From Date </label> 
       <input id="datepicker" name="fdate" class="element text medium" maxlength="10" value="" type="text"/> 
       </span> 
       </li> 
       <li> 
       <span> 
       <label class="description" for="element_4">Active </label> 
       <input name="r1" id="rad" class="element radio" type="radio" value="Yes" checked="checked" /> 
       <label class="choice">Yes</label> 
       <input name="r1" id="rad" class="element radio" type="radio" value="No" /> 
       <label class="choice">No</label> 
       </span> 
       </li> 

       <li> 
       <input type="button" name="save" value="Save" /> 
       </li> 

       <!-- 
       <li class="buttons"> 
        <input type="hidden" name="form_id" value="532698" /> 
        <input class="button_text" type="submit" name="save" value="Save" id="" /> 
        &nbsp;<input class="button_text" type="reset" name="cancel" value="Cancel" /> 
        &nbsp;<input class="button_text" type="submit" name="search" value="Search" id="" /> 
        &nbsp;<input class="button_text" type="submit" name="up" value="Update" id="" /> 
        &nbsp;<input class="button_text" type="submit" name="del" value="Delete" id="" /> 
       </li> 
       --> 
      </ul> 
      </form> 
    </div> 
    <img id="bottom" src="file:///C|/xampp/htdocs/track/images/bottom.png" alt=""/> 

    <script type="text/javascript" language="JavaScript"> 
//submit form 
document.order.submit(); 
</script> 
    </body> 

</html> 

Vui lòng hướng dẫn tôi làm cách nào tôi có thể thực hiện việc này ?? Tôi sẽ biết ơn bạn

Trả lời

0

Bạn có thể sử dụng mã này để ngăn chặn hình thức từ submiting đến máy chủ nếu xác nhận thất bại:

$("form").on("submit", function(event){ 
    if(!isValidForm($(this)){ 
    event.preventDefault(); 
    } 
}); 

supposingly isValidForm() phương pháp có hình thức chọn và trả về true nếu hình thức là hợp lệ và false nếu nó không phải.

0

sử dụng e.preventDefault(); hoặc return false;

+0

Và bạn có nghĩ rằng nó sẽ làm gì? –

+0

@VytautasButkus yup nếu u sử dụng 'return false;' nó sẽ làm điều đó? có bất kỳ nghi ngờ nào – Ghostman

+0

Bạn có thể giải thích một chút về cách OP có thể sử dụng đoạn mã đó .. –

0

Kiểm tra xác nhận là thành công hay không trong $ (function()) nếu không thì trả về false ở chỗ .. khác trở thành sự thật ...

<script> 
$(function() { 
    $("#datepicker").datepicker(); 
    if(!validation) { 
     return false; 
    } 
    return true; 
}); 
</script> 
0

Có 2 cách chính để thực hiện việc này: PHP (phía máy chủ) và javascript (phía máy khách).

Các giải pháp PHP mà không phải là sạch sẽ, bạn cần phải làm mới trang, kiểm tra các giá trị đó và thiết lập đúng value ở mỗi đầu vào (loại bỏ những người bạn muốn xóa)

Các javascript/giải pháp jQuery sạch hơn: thay vì sử dụng một nút gọi hàm javascript, hãy kiểm tra mọi đầu vào (nếu bạn cần thông tin máy chủ để kiểm tra chúng, bạn có thể sử dụng Ajax) và hiển thị lỗi, chỉ đặt các lỗi sai.

Cả hai cách đều ổn, nhưng phương pháp javascript dễ kiểm soát/cập nhật hơn và dễ sử dụng hơn vì phương pháp này không làm mới trang.

Các vấn đề liên quan