2013-10-25 82 views
1

這可能是一個非常愚蠢的問題,但我無法弄清楚該網站的javascript命令,根據文本字段是否爲空來決定是否填充文本字段實際上填滿了我的代碼在這裏。對輸入文本的文本字段使用Javascript命令

<html> 
<head> 
<title>Contact Us</title> 
<link rel="stylesheet" type="text/css" href="main.css"> 
<style type="text/css"> 
#main-container{ 
padding-left: 3px; 
padding-right: 3px; 
} 
#left-bar{ 
text-align: right; 
} 
#left-bar, #right-bar{ 
border: 2px solid gray; 
} 
#top-nav-bar{ 
border-top: 1px solid gray; 
border-bottom: 2px solid gray; 
border-left: 2px solid gray; 
border-right: 2px solid gray; 
} 
#navTextn{ 
float: right; 
padding-right: 20px; 
padding-top:10px; 
} 
#navTextc{ 
float: right; 
padding-right: 20px; 
padding-top:10px; 
} 
#navTexth{ 
float: right; 
padding-right: 20px; 
padding-top:10px; 
} 
#Contus{ 
float:right; 
padding-right: 30px; 
padding-top:10px; 
} 
#logo{ 
height: 20px; 
width: 100px; 
padding-top: 10px; 
padding left: 10px; 
} 


</style> 
<script type="text/javascript"> 
    function validator(){ 
     if(document.mUs.bfinderm.text) 
      alert("Thank you for your submission"); 
     else 
      alert("No text has been entered"); 
    } 
</script> 


<title>Tickit</title> 
</head> 
<body id="body"> 
<div id="topline"> 
</div> 
<div id="top-nav-bar"> 
    <a href="Homework.html"><image id="Logo" src="Tickit.png"></a> 
    <a id="Contus" href="ContactUs.html">Us</a> 
    <a id="navTextn">News</a> 
    <a id="navTextc" href="Calendar.html">Calendar</a> 
    <a id="navTexth" href="Homework.html">Home</a> 
</div> 
<table id="main-container"> 

    <tr> 
     <td id="left-bar" valign="top"> 
      <a id="plinks" >Profile</a><br> 
      <a id="plinks" >Mail</a><br> 
      <a id="plinks" href="Homework.html">Work Stream</a><br> 
      <a id="plinks" href="calendar.html">Calendar</a><br> 
      <a id="plinks" href="Events.html">Events</a><br> 
      </p> 
     </td> 
     <td id="main" valign="top"> 
     If you find any bugs with the website, please leave a message to us in the box below <br> <br> 
      <form name="mUs"> 
       Message Us:<br> <input type="text" name="bfinderm" /> 
       <input type="button" value="Send" onClick="validator()" /> 
      </form> 
     </td> 
     <td id="right-bar" valign="top"> 

     </td> 
    </tr> 
</table> 
</body> 
</html> 

就像一個複選框,驗證()的代碼將被選中

<script type="text/javascript"> 
    function validator(){ 
     if(document.stackflowrun.stflr.checked) 
      alert("Yes it is checked!"); 
     else 
      alert("Check that shit boul!"); 
    } 
</script> 

那麼,什麼是等效驗證器()命令文本字段的選擇嗎?

function validator(){ 
     if(document.mUs.bfinderm.text) 
      alert("Thank you for your submission"); 
     else 
      alert("No text has been entered"); 
    } 
</script> 

(文本不是它)

+0

如果您只是使用'val()'你可以看到他們輸入的內容。 – jamespick

回答

1

使用value,而不是text這樣:

function validator(){ 
     if(document.mUs.bfinderm.value) 
      alert("Thank you for your submission"); 
     else 
      alert("No text has been entered"); 
    } 
2

的jquery:$("#youridhere").val()返回輸入。 加入:<script src="http://code.jquery.com/jquery-1.9.1.js"></script>使用jquery。把這個放在你的<script>之前。

1

在JavaScript:

if (document.getElementById("bfindem").value.length > 0) 
{ 
       alert("Thank you for your submission"); 
     else 
} 
      alert("No text has been entered"); 
} 

在jQuery中:

if ($("#bfindem").val().length > 0) 
{ 
       alert("Thank you for your submission"); 
     else 
} 
      alert("No text has been entered"); 
} 
0

使用jQuery,修改你的功能如下圖所示

function validator(){ 
    if($('input[name="bfinderm"]').val().length !== 0) 
     alert("Thank you for your submission"); 
    else 
     alert("No text has been entered"); 
} 

jQuery DEMO

使用直接的JavaScript,修改你的功能在這裏

function validator(){ 
    if($('input[name="bfinderm"]').val().length !== 0) 
     alert("Thank you for your submission"); 
    else 
     alert("No text has been entered"); 
} 

Javascript DEMO

0

顯示嘗試document.mUs.bfinderm.value - jQuery的不需要