如果value为null,则在提交之前使用js或jQuery更改输入文本值 [英] change input text value with js or jQuery before submit if value is null
本文介绍了如果value为null,则在提交之前使用js或jQuery更改输入文本值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如果输入值为null,我如何使用js或jQuery在提交之前使用js或jQuery更改输入文本值?
How can I with js or jQuery change input text value with js or jQuery before submit if the input value is null ?
感谢您的帮助。
推荐答案
使用 jQuery ,你可以使用 .submit()
将回调绑定到提交事件方法。
With jQuery, you can bind a callback to the submit event with the .submit()
method.
$("form").submit(function(){
// Let's find the input to check
var $input = $(this).find("input[name=whatever]");
if (!$input.val()) {
// Value is falsey (i.e. null), lets set a new one
$input.val("whatever you want");
}
});
注意:为确保找到所有元素,应将其放入< a href =http://api.jquery.com/ready/ =noreferrer> domready 功能。
这篇关于如果value为null,则在提交之前使用js或jQuery更改输入文本值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文