繁体   English   中英

验证 jquery 的输入

[英]validate input for jquery

我没有表单,只是输入html:

<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>');}
    }});
});

简单,您可以在单击新产品按钮时调用 javascript 函数。

代替

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

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

在 validatefield() 函数中,您可以验证您的字段。

参考这个, http://jsfiddle.net/NWWL4/19/

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM