保留单击其单选按钮的文本框的值

Retain value of text box whose radio button is clicked

本文关键字:文本 单选按钮 单击 保留      更新时间:2023-09-26

我有不同的单选按钮,每个单选按钮在我的表单上都有一个与之关联的文本框值。用户一次只能选择一个单选按钮并提交表单。但是,有时用户会在文本框中为一个单选按钮输入数据,然后选择其他选项并在其中输入文本框数据。目前,它将数据保留在两个文本框中并提交它们。

有什么方法可以清除数据并仅保留最后单击的数据?就像一个onclick事件,它清除除选中按钮以外的任何其他单选按钮的所有数据。

只是一个快速的静态示例代码:

 <form class="form-horizontal" id="whereEntry" method='post' action=''>
    <input type="radio" name="formOption" id="radio1210" value="1210" checked="checked">Waiting on Approval<br>
    Comment:<input type="text" name="Comment"><br>
    <input type="radio" name="formOption" id="radio1211" value="1211" checked="checked">Waiting on Authorization<br>
     Comment:<input type="text" name="Comment">
     <input type="Submit" value="Submit">Submit
 </form>

单击单选按钮时将显示文本框值。因此,如果我单击单选按钮并在注释文本框中输入一个值,然后单击另一个单选按钮,则会保留两个注释框值。

像这样

Html 标记

<input type="radio" id="radio1" name="gender"/>Male
<input type="text" id="txt1">
<br />
<input type="radio" id="radio2" name="gender"/>FeMale
<input type="text" id="txt2">

Jquery

$("input:radio").click(function(){
    $("input:text").not($(this).next()).val("");
})

现场演示