简体   繁体   English

为什么对于使用复选框的HTML表单,此代码在jQuery / JavaScript中不起作用?

[英]Why won't this Code Work in jQuery/JavaScript for an HTML Form Working with Checkboxes?

Okay, this is kind of blowing my mind because I have used these bits of code separately and very successfully but when I put these together they don't work. 好的,这让我很吃惊,因为我已经分别并且非常成功地使用了这些代码,但是当我将它们放在一起时它们是行不通的。 I will explain what I am trying to do, although it will probably be pretty self-explanatory when you see the code below. 我将解释我要做什么,尽管当您看到下面的代码时,这可能很不言自明。

I am trying to simply make sure that only one of two checkboxes can be checked at a time for an HTML form. 我只是想确保一次可以检查HTML表单的两个复选框中的一个。 (Can't use radio button because the option for neither box to be checked must be available). (不能使用单选按钮,因为必须选中两个框都不可用)。

Anyway, here is the code (please assume that the ids of the input fields are accurate, they are "sIsGangMember" and "sIsNotGangMember" 无论如何,这是代码(请假定输入字段的ID正确,它们是“ sIsGangMember”和“ sIsNotGangMember”

$("#sIsGangMember").change(function () {

    if ($("#sIsGangMember").is(':checked')) {
        document.getElementById("sIsNotGangMember").checked = false;
    }
});

$("#sIsNotGangMember").change(function () {

    if ($("#sIsNotGangMember").is(':checked')) {
        document.getElementById("sIsGangMember").checked = false;
    }
});

I am really sorry if this is something stupid, but I know I have used all parts of this code separately before and all works great, but I simply can't see anything wrong with it nor do I see anything wrong with the HTML part: 如果这很愚蠢,我真的很抱歉,但是我知道我之前已经分别使用了这段代码的所有部分,并且都很好用,但是我根本看不到任何错误,也看不到HTML部分有错误:

<tr>
    <td class="lineupColor2">
        Search For Street<br/>Gang Member
    </td>
    <td class="lineupColor2">
        <label for="sIsGangMember">Is Gang Member</label>
        <br/>
        <input type="checkbox" id="sIsGangMember" name="sIsGangMember" 
            style="margin-left: 115px;" value="ticked" @IsGangMemberTicked/>
    </td>
    <td class="lineupColor2"> 
        <label for="sIsNotGangMember">Is Not Gang Member</label>
        <br/>
        <input type="checkbox" id="sIsNotGangMember" name="sIsNotGangMember"
            style="margin-left: 115px;" value="ticked" @IsNotGangMemberTicked/>
    </td>
    <td class="lineupColor2">
         <button type="button" style="cursor: pointer;" 
             onclick="clearIsGangMembers()">Clear Fields
         </button>
    </td>
    <td class="lineupColor2">
    </td>
</tr>

The razor (C#) that you see is simply a value that holds nothing or holds the string "checked='checked'" so that the form remembers the values of whether they are checked or not for further searches or if an error occurs and the form is not submitted, etc. 您看到的剃刀(C#)只是一个不包含任何值或包含字符串“ checked ='checked'”的值,以便表单记住是否检查它们的值以进行进一步的搜索或是否发生错误,并且表单未提交等

The part that assigns the C# variables are: 分配C#变量的部分是:

    //////////////////////////////////////////////////////////////////////////////////////////
    if(IsGangMember=="ticked"){IsGangMemberTicked="checked='checked'";}///////////////////////
    if(IsNotGangMember=="ticked"){IsNotGangMemberTicked="checked='checked'";}/////////////////
    //////////////////////////////////////////////////////////////////////////////////////////

Anyway, thanks for any help! 无论如何,谢谢您的帮助!

Thanks for all the help everyone, but can anyone tell me "why" this won't work? 感谢大家的所有帮助,但是谁能告诉我“为什么”这行不通?

UPDATE: 更新:

Okay, .click doesn't work either... clearly there is something else going on here... 好的,.click也不起作用...显然这里还有其他事情...

You will need to use click or mousedown instead of the change event. 您将需要使用click或mousedown而不是change事件。

jQuery checkbox change and click event jQuery复选框更改和单击事件

Ex. 例如 http://jsfiddle.net/qn8Kh/2/ http://jsfiddle.net/qn8Kh/2/

For some more information on why this is: jQuery difference between change and click event of checkbox 有关为什么的更多信息: 复选框的更改和单击事件之间的jQuery差异

Okay, I really appreciate the help, and somehow (after hours of searching and researching what could be wrong) I knew it would be something silly. 好的,我真的很感谢您的帮助,并且在某种程度上(经过数小时的搜索和研究可能出了问题的情况下),我知道这会很愚蠢。

Truth is, there is nothing wrong with using .change in this fashion. 事实是,以这种方式使用.change并没有错。

My problem was that I accidentally put the functions: 我的问题是我不小心放了函数:

$("#sIsGangMember").change(function () {

if ($("#sIsGangMember").is(':checked')) {
    document.getElementById("sIsNotGangMember").checked = false;
}
});

$("#sIsNotGangMember").change(function () {

if ($("#sIsNotGangMember").is(':checked')) {
    document.getElementById("sIsGangMember").checked = false;
}
});

All by themselves in the js file (eg, not wrapped in (document).ready or jQuery(function) or anything.... 全部独立于js文件中(例如,未包装在(document).ready或jQuery(function)或其他任何文件中。...

That's lame, yes, I know xD sry about that, and thnx for your help, everyone! s脚,是的,我知道xD很抱歉,感谢您的帮助!

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

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