0

我正在尝试清除Input字段的当前值,totalA并且qtyA当一个select框的选定值acct已更改时。

到目前为止我有这段代码,但不确定在哪里清除这些值。

<script language="javascript">
$(document).ready(function() {
    $("#acct").on('change', function() {
        var selVal = $(this).val();
        if (selVal == 'Full-Time') { // Full Time
            $('.parttime').hide();
            $('.fulltime').show();
            $('.agent').show();
            $('.error').hide();
        }
        else if (selVal == 'Part-Time') { // Part Time
            $('.parttime').show();
            $('.fulltime').hide();
            $('.agent').show();
            $('.error').hide();
        }
        else {
            $('.parttime').hide();
            $('.fulltime').hide();
            $('.agent').hide();
            $('.error').show();
        }
    });

    $('#qtyA').on('change', function() {
        var selVal = $("#acct").val();
        if (!isNaN($(this).val())) {
            var total = 0;
            if (selVal == 'Full-Time') {
                total = parseInt($(this).val()) * 1280;
            }
            else if (selVal == 'Part-Time') {
                total = parseInt($(this).val()) * 720;
            }
            $('#totalA').val(total.toFixed(2));
        }
        else {
            $(this).val('0');
             $('#totalA').val('0.00');
        }
    });
});
</script>

select箱码:

<p>
   <label for="acct" style="margin-right:45px;"><strong />Account Type<strong><font color="red" size="3"> * </font></strong></label>
   <select name="acct" style="background-color:white;"  id="acct" value="" class="validate[custom[cname]] text-input">
      <option value="Full-Time">Full-Time</option>
      <option value="Part-Time">Part-Time</option>
      <option selected="selected" value=""></option>
   </select>
</p>

Input字段:

<p>
<table class="agent">
    <tr>
        <td>
            <lable style="margin-right:89px;"># of Agent(s)<font color="red" size="3"> * </font></lable>
        </td>
        <td>
            <input style="width:25px; margin-left:5px;" type="text" name="qtyA" id="qtyA" />
        </td>
        <td>
            X  &nbsp; $<label id="acctFull Time" class="desc fulltime" style="display:none">1280</label>
            <label id="acctPart Time" class="desc parttime" style="display:none">720</label> = &nbsp;
        </td>
        <td>
            $<input style="width:65px; margin-left:5px;" type="text" readonly="readonly" name="totalA" id="totalA" onchange="calculate()" />
        </td>
    </tr>
</table>
</p>
<p class="error" align="center">Please select an Account Type.</p>
4

2 回答 2

1

我希望我确实让您正确...您想在和onChange of中重置值?如果我是对的,你可以试试这个:#totalA#qtyA #acct

$(document).ready(function() {
  $("#acct").on('change', function() {
    resetValues();

    var selVal = $(this).val();
    if (selVal == 'Full-Time') { // Full Time
        $('.parttime').hide();
        $('.fulltime').show();
        $('.agent').show();
        $('.error').hide();
    }
    else if (selVal == 'Part-Time') { // Part Time
        $('.parttime').show();
        $('.fulltime').hide();
        $('.agent').show();
        $('.error').hide();
    }
    else {
        $('.parttime').hide();
        $('.fulltime').hide();
        $('.agent').hide();
        $('.error').show();
    }
  });

  $('#qtyA').on('change', function() {
    var selVal = $("#acct").val();
    if (!isNaN($(this).val())) {
        var total = 0;
        if (selVal == 'Full-Time') {
            total = parseInt($(this).val()) * 1280;
        }
        else if (selVal == 'Part-Time') {
            total = parseInt($(this).val()) * 720;
        }
        $('#totalA').val(total.toFixed(2));
    }
    else {
        resetValues();
    }
  });

  function resetValues()
  {
     $('#qtyA').val('0'); // or set empty
     $('#totalA').val('0.00');   // or set empty
  }
});

正如您在输入无效值时重置值一样,#qtyA创建了一个函数,并在更改开始时添加了对它的调用eventHandler

于 2012-09-16T20:43:47.910 回答
0
<script language="javascript">
$(document).ready(function() {
    $("#acct").on('change', function() {
        var selVal = $(this).val();
        if (selVal == 'Full-Time') { // Full Time
        $('#qtyA').val('0'); // or set empty
            $('#totalA').val('0.00');   // or set empty
            $('.parttime').hide();
            $('.fulltime').show();
            $('.agent').show();
            $('.error').hide();
        }
        else if (selVal == 'Part-Time') { // Part Time
        $('#qtyA').val('0'); // or set empty
            $('#totalA').val('0.00');   // or set empty
            $('.parttime').show();
            $('.fulltime').hide();
            $('.agent').show();
            $('.error').hide();
        }
        else {
        $('#qtyA').val('0'); // or set empty
            $('#totalA').val('0.00');   // or set empty
            $('.parttime').hide();
            $('.fulltime').hide();
            $('.agent').hide();
            $('.error').show();
        }
    });

我没有把它放在 onchange 函数上,而是在 ready 函数中添加了脚本,所以当用户选择 div 时它会改变

于 2012-09-17T07:13:19.960 回答