[英]change input text value with js or jQuery before submit if value is null
如果輸入值為null,如何使用js或jQuery在提交之前使用js或jQuery更改輸入文本值?
感謝幫助。
使用純DOM(無庫),您的HTML將如下所示:
<form name="foo" action="bar" method="post">
<!-- or method="get" -->
<input name="somefield">
<input type="submit" value="Submit">
</form>
你的腳本看起來像這樣:
var form = document.forms.foo;
if (form && form.elements.something)
// I use onsubmit here for brevity. Really, you want to use a
// function that uses form.attachEvent or form.addEventListener
// based on feature detection.
form.onsubmit = function() {
// if (form.elements.foo.value.trim()) is preferable but trim()
// is not available everywhere. Note that jQuery has $.trim(),
// and most general purpose libraries include a trim(s)
// function.
if (form.elements.something.value.match(/^\s*$/)))
form.elements.something.value = 'DEFAULT VALUE';
};
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.