JQuery:如何检查表单字段的值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19701183/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
JQuery: How to check the value of a form field
提问by FrancescoMussi
I have a simple form, with one issue. In explorer, if nothing is inserted, the placeholder is passed as input of the field.
我有一个简单的表格,有一个问题。在资源管理器中,如果未插入任何内容,则占位符将作为字段的输入传递。
Here is JSbin: http://jsbin.com/EvohEkO/1/
这是 JSbin:http://jsbin.com/EvohEkO/1/
I would like to make a simple comparision, when form is submitted, to check if the value of the field is equal to "First name", and if yes make the value empty ""
我想做一个简单的比较,当提交表单时,检查该字段的值是否等于“名字”,如果是,则将该值设为空“”
Just this i need. Someone can help me please?
我需要的就是这个。有人可以帮助我吗?
回答by doniyor
<form onsubmit="return checkform()">
<input name="test" placeholder="placeholdertext" id="test" />
<input type="submit" value="submitbutton"/>
</form>
in js
在js中
you should import jquery latest version this is the link: http://code.jquery.com/jquery-1.10.2.min.js
你应该导入 jquery 最新版本这是链接:http: //code.jquery.com/jquery-1.10.2.min.js
function checkform(){
var fieldvalue = $.trim($('#test').val());
if(!fieldvalue || fieldvalue=="placeholdertext"){
alert('there is no input');
return false;
}else{
alert('enjoy your form!');
return true;
}
}
回答by Bharath R
This is somewhat easier..
这个比较容易。。
$(document).ready(function(){
$("#form").submit(function(){
$('#form input:text, textarea').each(function() {
if($(this).val()==$(this).attr('placeholder'))
$(this).val(" ");
});
});
});
回答by xjliao
You can do it like the following!
你可以像下面这样做!
<form>
<input type="text" id="first_name" name="first_name"/>
<input type="submit" id="submit" value="submit"/>
</form>
<script type="type/javascript"/>
jQuery.noConflict();
(function ($) {
$(function () {
$("#submit").click(function () {
if ($("#first_name").val() == "first name") {
$(this).val("");
}
});
});
})(jQuery);
</script>
回答by Majid Kalkatehchi
To check the value of a form field use the val() function on the input element
要检查表单字段的值,请使用输入元素上的 val() 函数
var input_value = jQuery('Input Element Selector').val();
As I looked to your form, the elements do not have any id attribute, I recommend that you add one to each of them, also change the submit input to button the you have more control on the javascript. so your form will look like :
当我查看您的表单时,这些元素没有任何 id 属性,我建议您为每个元素添加一个,并将提交输入更改为按钮,这样您就可以更好地控制 javascript。所以你的表格看起来像:
<form id="form" action="form.php" method="post">
<input id="fname" type="text" placeholder="First name" name="fname"><br>
<label for="fname" id="fnamelabel"></label>
<input id="lname"type="text" placeholder="Last name" name="lname"><br>
<textarea id="message" placeholder="Contact us!" cols="30" rows="5" name="message"></textarea><br>
<br>
<input type="button" value="Submit">
</form>
so your jQuery will look like:
所以你的jQuery看起来像:
jQuery(document).ready(function(){
jQuery('input[type="button"]').click(function(){
var fname = jQuery('#fname').val();
var lname = jQuery('#lname').val();
var message = jQuery('#message').val();
...... Do whatever you need & then change the input values
...... if all validation has passed the use jQuery('#form').submit(); to submit the form otherwise reset the form:
jQuery('#fname').val("");
jQuery('#lname').val("");
jQuery('#message').val("");
});
});
here is a working version: jsfiddle working link
这是一个工作版本: jsfiddle working link
回答by kaushik0033
Just put your field value in hidden type input like this :-
只需将您的字段值放在隐藏类型输入中,如下所示:-
<input id="hdnfield" name="hdnfield" value="<Your Field Value>" />