jQuery单选按钮隐藏输入值的变化

时间:2015-06-02 09:01:18

标签: javascript jquery html

我有一个单选按钮,允许用户选择一个选项,为此我根据这个选择做了一个隐藏的输入来保存一个值。

表格如下:

private static void _fixAliases(KeyStore keyStore) {
    Field field;
    KeyStoreSpi keyStoreVeritable;

    try {
        field = keyStore.getClass().getDeclaredField("keyStoreSpi");
        field.setAccessible(true);
        keyStoreVeritable = (KeyStoreSpi)field.get(keyStore);

        if("sun.security.mscapi.KeyStore$MY".equals(keyStoreVeritable.getClass().getName())) {
            Collection entries;
            String alias, hashCode;
            X509Certificate[] certificates;

            field = keyStoreVeritable.getClass().getEnclosingClass().getDeclaredField("entries");
            field.setAccessible(true);
            entries = (Collection)field.get(keyStoreVeritable);

            for(Object entry : entries) {
                field = entry.getClass().getDeclaredField("certChain");
                field.setAccessible(true);
                certificates = (X509Certificate[])field.get(entry);

                hashCode = Integer.toString(certificates[0].hashCode());

                field = entry.getClass().getDeclaredField("alias");
                field.setAccessible(true);
                alias = (String)field.get(entry);

                if(!alias.equals(hashCode)) {
                    field.set(entry, alias.concat(" - ").concat(hashCode));
                } // if
            } // for
        } // if
    } catch(Exception exception) {
        System.err.println(exception);
        exception.printStackTrace();
    } // catch
} // _fixAliases

jQuery就像这样:

<form action="#" method="post">
    <label for='radio1'>Select this?&nbsp;&nbsp;</label>
    <input type='checkbox' id='radio1' name='radio1' />

    <input type="hidden" id="value1" name="value1" value="" />

    <input type='submit' name='submit' value='Submit' />
</form>

我不知道我在哪里或者如何解决它做错了,有人能指出正确的方法吗?

3 个答案:

答案 0 :(得分:2)

您需要检查已检查属性的值

$(document).ready(function () {
    $("#radio1").on("click", function () {
        $("#value1").val(this.checked);
    });
});

演示:Fiddle

答案 1 :(得分:0)

.value

您不需要value1
var value1 = $("#value1"); // Don't need `.value` here

if ($("#radio1").is(":checked")) {
    value1.val("true");
} else {
    value1.val("false");
}

答案 2 :(得分:0)

这是正在运行的代码

   $(document).ready(function() {
     $("#radio1").on("click", function() {

       if ($("#radio1").is(":checked")) {
         alert($("#radio1").attr("checked", true).is(":checked"));
       } else {
         alert($("#radio1").attr("checked", false).is(":checked"));
       }
     });
   });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<label for='radio1'>Select this?&nbsp;&nbsp;</label>
<input type='checkbox' id='radio1' name='radio1' />

<input type="hidden" id="value1" name="value1" value="" />

<input type='submit' name='submit' value='Submit' />