激活后,按钮不会执行操作

时间:2012-07-13 18:59:59

标签: javascript jquery html

我不明白为什么按钮在启用复选框后不会变为活动状态。我已经让它工作而没有禁用提交按钮,但它的工作独立于被检查的复选框

<!doctype html>
<html lang="en">

<head>
<meta http-equiv="Pragma" content="no-cache">

<meta HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1">

<meta charset="utf-8" />
<meta name="viewport" content="initial-scale=1.0, maximum-scale=1.0, user-                  
scalable=0, width=device-width;">
<title>Welcome to Tiffany &amp; Co.</title>

<link rel="stylesheet" href="index.css" type="text/css" media="screen" />

<script src="http://code.jquery.com/jquery-1.7.2.min.js"></script>

<!--Enable media queries in some unsupported subrowsers-->
<script type="text/javascript" src="css3-mediaqueries.js"></script>

<script type="text/javascript" src="main.js"></script>
<!--[if lt IE 9]>
<script src="http://html5shiv.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->

</head>

<body>
<div id="wrapper">

<script>
function submitAction() 
{

var link = document.location.href;

var searchString = "redirect=";

var equalIndex = link.indexOf(searchString);

var redirectUrl = "";

  if (document.forms[0].action == "") 
    {

    var url = window.location.href;

    var args = new Object();

    var query = location.search.substring(1);

    var pairs = query.split("&");

    for(var i=0;i<pairs.length;i++)
        {

        var pos = pairs[i].indexOf('=');

        if(pos == -1) continue;

        var argname = pairs[i].substring(0,pos);

        var value = pairs[i].substring(pos+1);

        args[argname] = unescape(value);

        }

    document.forms[0].action = args.switch_url;       

    }       

if(equalIndex >= 0) 
    {

    equalIndex += searchString.length;

    redirectUrl = "";

    redirectUrl += link.substring(equalIndex);

    }

if(redirectUrl.length > 255)

redirectUrl = redirectUrl.substring(0,255);

document.forms[0].redirect_url.value = redirectUrl;

document.forms[0].buttonClicked.value = 4;

document.forms[0].submit();

}

function reject()
{
alert("You will not be able to access the system!");
}

function loadAction() {
var url = window.location.href;
var args = new Object();
var query = location.search.substring(1);
var pairs = query.split("&");
for (var i = 0; i < pairs.length; i++) {
    var pos = pairs[i].indexOf('=');
    if (pos == -1) continue;
    var argname = pairs[i].substring(0, pos);
    var value = pairs[i].substring(pos + 1);
    args[argname] = unescape(value);
}
document.forms[0].action = args.switch_url;
}
</script>

<form method="post"> 
<input TYPE="hidden" NAME="buttonClicked" SIZE="16" MAXLENGTH="15" value="0"> 
<input TYPE="hidden" NAME="redirect_url" SIZE="255" MAXLENGTH="255" VALUE=""> 
<input TYPE="hidden" NAME="err_flag" SIZE="16" MAXLENGTH="15" value="0">





<h1>Welcome to Tiffany &amp; Co.</h1>
<p id="desc">We're delighted to offer our customers complimentary Wi-Fi     
while shopping in our store.</p>
<form>
<p id="terms">
    <input id="termsCheckbox" type="checkbox" 
onclick="this.form.submit.disabled = !this.checked"/> I have read and agreed to the <a 
href="terms.html">Terms of Use</a>
</p>
<p id="error">
    Please agree to the Terms of Use by checking the box.
</p>
<p class="connect">
<input id="connect" type="button" name="submit" value="Accept"  
class="button" disabled="disabled" onclick="submitAction();">
</p>
</form>

<div id="logo">
    <img src="logo.png" width="108" height="14" alt="Tiffany &amp; Co."       
/>
</div>
</div>
</body>

</html>

2 个答案:

答案 0 :(得分:2)

你的代码工作正常,这很乱,但它确实有效。问题是:

Uncaught ReferenceError: submitAction is not defined

问题的原因是您在submitAction()

上没有右括号

这是一个working demo。我也cleaned up your code

正确格式化和缩进代码非常重要。它不仅看起来很好而且更容易阅读,你很容易发现不匹配/缺失的括号。

答案 1 :(得分:0)

试试这个

$(function(){
   $('#termsCheckbox').change(function(){
      $('#connect').prop('disabled', !this.checked);
   } 
});