在函数goForit()中添加一个类

时间:2013-10-14 23:41:22

标签: 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 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>

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

我想通过重定向传递.carrot类,因此会显示正确的图像。如何将.carrot类添加到以下代码中?

2 个答案:

答案 0 :(得分:2)

看看这里:

How to redirect on another page and pass parameter in url from table?

HTML:

<input type="button" name="theButton" value="Detail" class="btn" data-username="{{result['username']}}" />

然后检查重定向网址的位置:

使用Javascript:

$(document).on('click', '.btn', (function() {

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

    if (name != undefined || name != null) {
        window.location = '/player_detail?username=' + name;
    }
});​

另外,另一个StackOverflow问题:

Javascript redirect and Pass Argument to redirected Page

答案 1 :(得分:1)

你已经接受了答案,但是这里有一个更接近原版并且不需要jQuery的替代方案:

<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…');
  }
}