English 中文(简体)
如果是其他的话,我如何在 Java写这句话? [闭门]
原标题:How do I write this in JavaScript if else? [closed]
  • 时间:2012-01-12 18:29:31
  •  标签:
  • javascript

I am looking to have a password field with a submit button that on click of submit, if the password enter is not 12345, and error message will appear, but if 12345 is entered, it will go to www.URL.com

最佳回答
 function checkIfValid(){
     var yourField = document.getElementById( password ).value;
     if (yourField == "12345") { 
          window.location.href = "http://www.google.com"; 
          return true;
      }else{
          alert("NOT GOOD");
          return false;
      }
  }

<form name="FORMNAME" action="script.php" onSubmit="return checkIfValid()">
    <input name="password" id= password  type="password"/>
    <input type="submit" value= submit >
</form>
问题回答

为探测纽扣吨,可使用<代码>onclick:

document.getElementById("button").onclick = function() {
    //code here
};

然后,为了获得投入的价值,你可以使用<代码>。

var value = document.getElementById("password").value;

比较一下,使用<条码>,如果是,平等操作者:

if (value == "12345")

You could show an "error message" with alert if the password is wrong:

alert("wrong password");

使用<条码>分处。

location.href = "www.url.com";

请注意,完全依赖客户方的认证部分(例如,浏览器 Java本)根本不可靠。

该守则是:

<><><><>>><>>>>><>>>>>>

<input type="password" id="pass" />
<input type="button" id="foo" value= Click me  />

<>JS

function addHandler(type, el, myFunction) {
    if (el.addEventListener) {
        el.addEventListener(type,myFunction,false);
    } else (el.attachEvent) {
        type =  on  + type;
        el.attachEvent(type,myFunction);
    }
}
var button = document.getElementById( foo );

addHandler( click , button, function () {
    var pass = document.getElementById( pass ).value;
    if (pass ===  12345 ) {
        window.location =  http://url.com/ ;       
    } else {
        alert( not so bad );
    }
});

当然,你可以把纽子改变为提交,没有很大的区别。

And the fiddle:

regards!

如果你们100%知道你正在做什么——我相信你不会这样做——那么:

<form action="" method="post" 
  onsubmit="if(passwd.value== myPass )location= http://example.com/ ;return false">
 <input type="password" name="passwd" />
 <button type="submit">Submit</button>
</form>

此外,我知道该编码是多余的(passwd,而不是添加一个贴现和打电话的<编码>文件.getElementById(通过wd ),或至少使用this.passwd),但通知是个排第一的;]





相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.

热门标签