javascript - 如果值为空,则在提交之前使用 js 或 jQuery 更改输入文本值

标签 javascript jquery

如果输入值为空,我如何使用 js 或 jQuery 在提交之前使用 js 或 jQuery 更改输入文本值?

感谢您的帮助。

最佳答案

jQuery ,您可以使用 .submit() 将回调绑定(bind)到提交事件方法。

$("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");
   }
});

注意:为确保可以找到所有元素,应将其放在 domready 中功能。

关于javascript - 如果值为空,则在提交之前使用 js 或 jQuery 更改输入文本值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9943968/

相关文章:

javascript - ajax查询中从div获取数据

javascript - 在新窗口中打印 Canvas

javascript - 遍历 DOM 以确定父级的状态。根据条件返回字符串

javascript - ajax加载后无法刷新某一特定div(MVC系统)

javascript - 以函数为参数的函数

jquery - jQuery 可以确定哪些 div 当前位于用户的浏览器 View 中吗?

javascript - 左右箭头键对我不起作用

javascript - 我需要帮助才能在 Xcode 中创建的简单 webview 应用程序中运行 Javascript Alert 和 Confirm

javascript - jQuery 中的 foreach 对象/数组

javascript - 如何在打开事件时获取放大弹出 API 的 url?