Javascript 将类添加到函数goForit()中

Javascript 将类添加到函数goForit()中,javascript,Javascript,我有以下代码: function goForit() { var passwd; passwd = document.forms['giftForm'].pass.value; if(passwd=="yes"){ window.open ('eat.html','_self',false) } else{ alert('Password wrong'); } } <form id="giftForm"> <input

我有以下代码:

function goForit() {
  var passwd;
  passwd = document.forms['giftForm'].pass.value;
  if(passwd=="yes"){
  window.open ('eat.html','_self',false)
  }
  else{
  alert('Password wrong');
  }
}

<form id="giftForm">        
    <input type="text" id="pass" placeholder="" style="font-size:150%;"/>
    <br />
    <input type="button" onClick="goForit()" value="Submit" style="font-size:100%; margin-top:15px;" />                 
 </form>
函数goForit(){
var-passwd;
passwd=document.forms['giftForm'].pass.value;
如果(passwd==“是”){
window.open('eat.html','u self',false)
}
否则{
警报(“密码错误”);
}
}

如果有人输入“是”作为密码并单击提交按钮。用户将被重定向到eat.html页面

我想通过重定向的.carrot类,这样就会出现正确的图像。如何将.carrot类添加到以下代码中?

请看这里:

Html:

另外,还有一个问题:


您已经接受了答案,但这里有一个更接近原始答案的替代方案,不需要jQuery:

<button type="button" value="goForit(this)" value="carrot" ... >Go for it</button>

如果您也在使用PHP,那么可以使用GET传递它。不确定是否可以只使用javascript。哦,天哪,请告诉我这不是你验证用户的方式。@xbonez甚至都不要提起这件事……我会死的,哈哈。@xbonez这是永远不会看到生产的东西。它只用于5分钟的演示,然后所有内容都会被划破。@EliteGamer在重定向中,我能做一些类似body class=“carrot”的操作让它在加载时触发吗?@tymeJV-ha,对不起,让我用JavaScriptThink重新编写,我想我会在大量否决票到来之前通知你:DSince
undefined==null
,第二个表达式是多余的。
$(document).on('click', '.btn', (function() {

    var name = $(this).data('username');

    if (name != undefined || name != null) {
        window.location = '/player_detail?username=' + name;
    }
});​
<button type="button" value="goForit(this)" value="carrot" ... >Go for it</button>
function gotForit(el) {
  if (el.form.pass.value.toLowerCase() == 'yes') {
    window.location = 'eat.html?username=' + el.value;
  } else {
    alert('oops…');
  }
}