简体   繁体   English

javascript无法在所有浏览器中正常运行

[英]javascript not working in all browsers

I have looked all over the place and have tried all kind of scripts but i keep running into the same problem. 我到处都看过了,尝试过各种脚本,但是我一直遇到同样的问题。

I am trying to disable or hide a submit button until a checkbox is checked. 我试图禁用或隐藏提交按钮,直到选中了复选框。

The problem I am having is that nothing is working in IE10 unless I have the compatibility turned on, and they do not work in FF 20.0.1 most of the time. 我遇到的问题是,除非启用了兼容性,否则IE10中什么都不会起作用,并且它们大多数时间都无法在FF 20.0.1中工作。

Here is what I have tried so far. 到目前为止,这是我尝试过的。


    <input type="checkbox" name="agree" value="yes" onclick="agreed.disabled = !this.checked" />Do you Agree? &nbsp;
    <input type="submit" name="agreed" id="agreed" value="{L_AGREE}" class="button1" disabled="disabled" />

<script type="text/javascript">
function checkSubmit(ele, id) {
    x = document.getElementById(id);
    if (ele.checked == true) x.disabled = false;
    else x.disabled = true;
}
</script>

<input type="checkbox" name="myCheck" onclick="checkSubmit(this, 'agreed')" value="y" />Do you Agree? &nbsp;
<input type="submit" name="agreed" id="agreed" value="{L_AGREE}" class="button1" disabled="disabled" />

<script type='text/javascript' src='http://code.jquery.com/jquery-1.7.1.js'></script>
<script type='text/javascript'>//<![CDATA[ 
$(window).load(function(){
$("#terms").click(function() {
    if ($(this).is(':checked')) {
        $('#agreed').removeAttr('disabled');
    } else {
        $('#agreed').attr('disabled', 'disabled');
    }
});

});//]]>  

</script>

            <input type="checkbox" class="required" id="terms"/>Do you Agree? &nbsp;
            <input type="submit" name="agreed" id="agreed" value="{L_AGREE}" class="button1" disabled="disabled" />

I have tried many many other ways to do it but still running into problems with either IE10 or FF. 我尝试了许多其他方法来执行此操作,但仍然遇到IE10或FF的问题。

What I need is help with getting something to work in all browsers. 我需要的是在所有浏览器中正常工作的帮助。

The first example is more likely to work some browsers and not others depending on if they put elements on the window or document objects via their ID. 第一个示例更可能适用于某些浏览器,而不适用于其他浏览器,这取决于它们是否通过ID将元素放在windowdocument对象上。

To fix it, use document.getElementById() . 要修复它,请使用document.getElementById()

<input type="checkbox" name="agree" value="yes" onclick="document.getElementById('agreed').disabled = !this.checked" />Do you Agree? &nbsp;
<input type="submit" name="agreed" id="agreed" value="{L_AGREE}" class="button1" disabled="disabled" />

The second one should work in any browser as long as the function is global, though you can shorten it. 只要该函数是全局函数,第二个函数就可以在任何浏览器中运行,尽管可以将其缩短。

function checkSubmit(ele, id) {
    document.getElementById(id).disabled = !ele.checked;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM