选择框选择索引后,清除输入文本框字段已更改

时间:2012-09-16 19:06:24

标签: javascript jquery

InputtotalA的选定值已更改时,我正在尝试清除qtyA字段selectacct的当前值

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

<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>

2 个答案:

答案 0 :(得分:1)

我希望我确实帮到你了...你想在#totalA#qtyA onChange {{{}> 重置值 1}?如果我是对的你可以试试这个:

#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 } }); {<}}} 创建了一个功能并且添加了一个电话确实重置了输入无效值的值它在更改eventHandler

的开始

答案 1 :(得分: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函数上 我在准备好的函数中添加了脚本,因此它在何时更改 用户选择div