在RadioButton更改(jQuery)上保存文本值



假设我有8个无线电按钮,当选择了第8个按钮时,出现一个文本字段,您可以在其中输入某些内容。现在,如果在Textfield中输入了某些内容并检查了另一个广播按钮,我想保存文本,但要清空TextField,当它们再次重新检查第8个时,Textfield会填充先前输入的数据。

我尝试了以下内容:

referenceRadio.change(function() {
    otherTextValue = otherText.val();
    console.log(otherTextValue);
    if($(this).val()=="8") {
        otherText.show().prop('required', true);
        otherText.val(otherTextValue);
    } else {
        otherText.val('');
        otherText.hide().prop('required', false);
    }
});

如果需要任何其他信息,请询问,纽约。

预先感谢!

更新:

设法使其与以下代码一起使用:

referenceRadio.change(function() {
    console.log(otherText.val());
    if($(this).val()=="8") {
        otherText.show().prop('required', true);
        otherText.val(otherTextValue);
    } else {
        if(otherText.val() != '') {
            /* Only retrieve otherText.val when its not empty */
            otherTextValue = otherText.val();
        }
        otherText.val('');
        otherText.hide().prop('required', false);
    }
});

通过:

  1. 当前选择了8个以外的单选按钮。
  2. 现在您单击8,这发生了:

    otherTextValue = otherText.val();
    
  3. 现在otherTextValue""

如果您将其放入else子句中,则在清空文本字段之前将其工作,并且应该将该子句作为else if,以便仅在上次选择8时执行它:

referenceRadio.change(function() {
    if($(this).val() == "8") {
        otherText.show().prop('required', true);
        otherText.val(otherTextValue);
    } else if(lastRadioValue == "8") {
        otherTextValue = otherText.val();
        otherText.val('');
        otherText.hide().prop('required', false);
    }
    lastRadioValue = $(this).val();
});

如果仅是第8个广播,您可以尝试以下操作:

var msgText='';
referenceRadio.change(function() {
    if($(this).val()=="8") {
        otherText.val(msgText).show().prop('required', true);
    } else {
        msgText=otherText.val();
        otherText.val('').hide().prop('required', false);
    }
});

最新更新