[英]How to disable and enable a radio button at the same time
I have a few radio buttons which are displayed from mysql database. 我有几个单选按钮,它们从mysql数据库显示。 What I'm trying to achieve is when I click the submit button after selecting a radio button, it is disabled and when I click another radio button, the previously disabled one will be enabled and the currently selected radio button will be disabled.
我要实现的目标是,当我选择一个单选按钮后单击“提交”按钮时,它被禁用,而当我单击另一个单选按钮时,将启用先前禁用的按钮,而当前选中的单选按钮将被禁用。 My code for displaying the buttons:
我显示按钮的代码:
<form action="">
<?php
foreach ($sql->results() as $sql) {?>
<input type="radio" name="session_id" id="cur" value="<?php echo $sql->id; ?>"> <?php echo $sql->title;?><?php
}?>
<input type="submit" value="submit" onclick="set_type('a')">
</form>
<?php
}
?>
I have tried to enable and disable some radio buttons on jsfiddle but it only works with different ids whereas my radio buttons have only one id. 我试图在jsfiddle上启用和禁用一些单选按钮,但它只能与不同的ID一起使用,而我的单选按钮只有一个ID。
EDIT : It has been made clear, ids must be unique. 编辑 :已经明确了,ID必须是唯一的。 I have changed that.
我已经改变了。
I have two suggestions. 我有两个建议。
I'm a little confused by exactly what you are trying to do, but I'll give a solution for each interpretation. 我对您要尝试执行的操作感到有些困惑,但是我将为每种解释提供一个解决方案。 :)
:)
You should be able to use this code to get the effect that you want, without needing to use any id
or name
attributes: 您应该能够使用此代码来获得所需的效果,而无需使用任何
id
或name
属性:
Approach #1: If you are wanting the buttons to be disabled when its associated radio button is selected, then use this code: 方法1:如果希望在选择与其关联的单选按钮时禁用这些按钮,请使用以下代码:
$(":radio").on("change", function() {
$(":submit:disabled").prop("disabled", false);
$(this).next(":submit").prop("disabled", true);
});
Approach #2: If the buttons are supposed to only disabled after they have been clicked, then use this code: 方法2:如果仅应在单击按钮后将其禁用,则使用此代码:
$(":radio").on("change", function() {
$(":submit:disabled").prop("disabled", false);
});
. 。 .
。 .
。 and let the
click
functionality that is (presumably) tied to the button, handle disabling it. 并让(大概)与按钮相关的
click
功能处理禁用它。
Explanation: Basically, any time you click a radio button, you want to turn of the disabled
property of any currently disabled buttons . 说明:基本上,每次您单击单选按钮时,您都希望打开任何当前禁用的按钮的
disabled
属性。 . 。 .
。 the easiest way to do that is to select all of the disabled buttons and change the
disabled
property. 最简单的方法是选择所有禁用的按钮并更改
disabled
属性。 Once that is done, you can re-apply the disabled
status to the one associated with the radio button (or, do that, when the button is clicked . . . whichever one matches what you are trying to accomplish :) ). 完成此操作后,您可以将
disabled
状态重新应用到与单选按钮关联的状态(或者,当单击该按钮时,执行此操作……与您要实现的目标匹配的任何一种:))。
Note: this is also assuming that you don't have any other radio buttons and submit buttons on the page . 注意:这也是假设您在页面上没有其他任何单选按钮和提交按钮。 .
。 .
。 if you do, you will need to "scope" the jQuery selectors.
如果这样做,则需要“范围”化jQuery选择器。 The easiest way to do this would be to give your
<form>
element and id
(I'll use "FORM_ID" for this example) and then update the code to this: 最简单的方法是提供
<form>
元素和id
(在本示例中,我将使用“ FORM_ID”),然后将代码更新为:
$("#FORM_ID").find(":radio").on("change", function() {
$("#FORM_ID").find(":submit:disabled").prop("disabled", false);
$(this).next(":submit").prop("disabled", true);
});
EDIT: 编辑:
Based on your clarification, this code should accomplish what you are looking for: 根据您的澄清,此代码应完成您要寻找的内容:
$(":submit").on("click", function() {
$(":submit:disabled").prop("disabled", false);
$(this).prop("disabled", true);
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.