2014-04-14 94 views
-2

我haven`t形式,僅僅只有輸入HTML:驗證輸入的jQuery

<script src="index.js"></script> 
</head> 
<body> 
<p>name</p> 
<input type="text" size="15" id="title" placeholder="enter title" onclick="check(this)"> 
<p>description</p> 
<textarea rows="7" cols="20" id="description" placeholder="enter description"> </textarea> 
<p>price</p> 
<input type ="number" value="00.0" min="0" step="0.1" id="price"><br> 
<button id="add_new_product" >Add new product</button><br> 
<a href="catalog.html">All product</a> 
</body> 

我必須驗證空了此輸入。 我的js文件:

$(function(){ 

    $('#add_new_product').click(function(){ 

     var title = $("#title").val(); 
     var description = $("#description").val(); 
     var price = $("#price").val(); 

     $.ajax({url:'adres/add_new_product',type:'GET', data:{description:description,title:title, price:price},success:function(result){ 
    alert('Product was add!)');   } 
     }); 

    }); 
}); 

$(document).ready(function(){ 

    $.ajax({url:'adres/all_products', type: 'GET', dataType: 'json', success:function(result){ 
     for(var i = 0; i < result.length; i++){ 
      $('#list_of_products').append('<b>' + result[i].name + '</b><br>' + result[i].description + '</br><br>'+result[i].price+'<br><br>');} 
    }}); 
}); 
+1

你可以請發表你自己的嘗試來解決這個問題。 –

回答

0

簡單,你可以調用JavaScript函數時,新產品的按鈕,同時。

<button id="add_new_product" onClick="javascript: validatefield()" >Add new product</button><br> 
在validatefield

(更換

<button id="add_new_product" >Add new product</button><br> 

)函數,你可以驗證你的領域。

推薦這個,http://jsfiddle.net/NWWL4/19/

+0

提醒工作,但產品後警告錯誤添加 – kuka

+0

參考http://jsfiddle.net/NWWL4/20/,這將解決您的proplem – Birlla

+0

我必須驗證所有我得到產品的領域。如果在輸入中會返回錯誤,我的ajax請求不起作用 – kuka