单选按钮更改事件在错误的情况下触发消息

时间:2014-10-04 10:54:44

标签: javascript jquery

我正在使用简单的单选按钮更改事件,因此当单击按钮时,它应评估其中一个条件。然而,正在发生的事情是,'notif'仍在触发msg,即使条件为'New Intake'。只有在条件=='New Intake with Files'时才会触发msg。

似乎完全无视这种情况。我一直在努力解决这个问题,如果有人能指出我的错误,我将不胜感激。非常感谢。

$(function() { 

    $('input:radio[name="activity"]').change(function(){
    if($(this).val() == 'New Intake with Files'){

       //alert("new intake with files");

         $('#box_add').on('blur',function(){
         $(this).attr('readonly','readonly'); //or use disabled
         $('#bfile_add').removeAttr('disabled'); //or use readonly

         // SHOULD ONLY DISPLAY IF THIS CONDITION IS MET
         notif({
                msg: "Please Only input 1 box per file submission. Each box will hold approx 20 files. Thank you.",
                type: "boxdstrError",
                position: "center",
                width: 490,
                height: 75,
                multiline: true,
                timeout: 6000,
                opacity: 0.8,
                fade: 10,
           });
         });
        } else 
    if($(this).val() == 'New Intake'){

       //alert("new intake");
       // SHOULD NOT DISPLAY IF THIS CONDITION IS MET
        $('#box_add').on('focus',function(){
        $(this).removeAttr('readonly'); //or use disabled attribute
        $('#bfile_add').attr('disabled', 'disabled'); //or use readonly

       });
       }
     });
  });

HTML

<div class="fieldset">
    <h1><span>Activity Type</span></h1>
      <p>
        <input id="activity" name="activity" class="css-checkbox" type="radio" value="New Intake" checked="checked" />
        <label for="activity" class="css-label"></label>
        <a href="javascript:void(0)" class="tooltip" title="Choose this option to put new boxes into storage.">New Intake</a>
        <br />

        <input id="activity1a" name="activity" type="radio" class="css-checkbox" value="New Intake with Files" />
        <label for="activity1a" class="css-label"></label>
        <a href="javascript:void(0)" class="tooltip" title="Choose this option to put new files into storage together with your box.">New Intake With Files</a>
      </p>
</div>

<input class="boxadd" id="box_add" name="box_add[]" type="text" required="required" style="width:250px; height:30px;"  />
<input name="bfile_add[]" id="bfile_add" type="text" disabled style="width:250px; height:30px;" />

1 个答案:

答案 0 :(得分:1)

单击“New Intake with Files”单选按钮后,每次notif输入失去焦点时,都会调用boxadd函数。所以,如果我没错,你想要的是在选择'New Intake'收音机时关闭blur处理程序:

if($(this).val() == 'New Intake') {
    $('#box_add').off('blur')
    ...
}