function check_empty() {
if (document.getElementById('CompanyName').value == "" || document.getElementById('ContactName').value == "" || document.getElementById('Address').value == "" || document.getElementById('PhoneNumber').value == "" || document.getElementById('Email').value == ""){
alert("Fill All Fields !");
} else {
/*want to add a class="my" */
alert("Form Submitted Successfully...");
}
}
/*html code*/
<a href="javascript:%20check_empty()" class="my">Submit to download</a>
当我点击链接&#34;提交下载&#34; &#34; check_empty()&#34;被叫,如果所有的filds都是空的,它会询问&#34;填写所有字段!&#34;否则我想打电话给一个班级&#34;我的&#34;,现在是html的元素。如何在js上添加它?
答案 0 :(得分:0)
<强>更新强>
我认为你在寻找的是如何使用attr placeholder
,required
和autofocus
你不需要javascript来检查输入是否为空尝试这样:< / p>
<form>
<input id=CompanyName type=text placeholder=CompanyName required autofocus />
<input id=ContactName type=text placeholder=ContactName required />
<input id=Address type=text placeholder=Address required />
<input id=PhoneNumber type=text placeholder=PhoneNumber required />
<input id=Email type=text placeholder=Email required />
<input type=submit value=send />
</form>
&#13;
现在,您可以在表单准备就绪时执行某些操作
function sendMyForm(event){
alert("Form Submitted Successfully...");
}
var sendForm = document.querySelector("form");
sendForm.addEventListener("submit", sendMyForm, false);
&#13;
<form>
<input id=CompanyName type=text placeholder=CompanyName required autofocus />
<input id=ContactName type=text placeholder=ContactName required />
<input id=Address type=text placeholder=Address required />
<input id=PhoneNumber type=text placeholder=PhoneNumber required />
<input id=Email type=text placeholder=Email required />
<input type=submit value=send />
</form>
&#13;
使用classList
document.querySelector("#CompanyName").classList.add('my');
所以你的代码看起来像这样
function check_empty() {
if (document.getElementById('CompanyName').value == "" || document.getElementById('ContactName').value == "" || document.getElementById('Address').value == "" || document.getElementById('PhoneNumber').value == "" || document.getElementById('Email').value == ""){
alert("Fill All Fields !");
} else {
/*want to add a class="my" */
document.querySelector("#CompanyName").classList.add('my');
/*document.getElementById("CompanyName").classList.add('my')*/
alert("Form Submitted Successfully...");
}
}
答案 1 :(得分:0)
您是否询问如何通过单击::
调用两个javascript函数如果是这样的话......试试这个......
<a href="javascript:void()" onClick="return check_empty() ? my() : false"> Submit to download </a>