如何让Ajax更新数量输入?

时间:2014-03-19 12:08:44

标签: jquery ajax

我需要从选择选项下拉表单中获取价格输入金额。我有jQuery将从下拉列表中选择选择值,我知道我需要使用Ajax来执行此操作,我不确定Ajax的机制以及如何执行此操作。我能指点一下吗?

<select id="selectMembership" class="cat_dropdown " name="selectMembership">
    <option type="text" value="5" selected="selected" id="amount-paypal" name="amount">5</option>
    <option type="text" value="10" id="amount-paypal" name="amount">10</option>
    <option type="text" value="20" id="amount-paypal" name="amount">20</option>    
</select>

This input needs to receive the selected value    
<input type="text" value="" id="amount-paypal" name="amount">

<script type="text/javascript">
    $(document).ready(function(){              

        //choose which price to use from select form
        $('select').on('change', function(){
            $('#amount-paypal').val($(this).val());
        });
    });
</script> 

2 个答案:

答案 0 :(得分:1)

试试这个

选择器id必须是唯一的,更改文本框的id现在它将根据选项框更改显示价格

HTML

<select id="selectMembership" class="cat_dropdown " name="selectMembership">
<option type="text" value="5" selected="selected"  name="amount">5</option>
<option type="text" value="10"  name="amount">10</option>
<option type="text" value="20"  name="amount">20</option>    
</select>
<input type="text" value="" id="amount-paypal-input" name="amount">

脚本

$(document).ready(function(){              
       $('select').on('change', function(){
            $('#amount-paypal-input').val($(this).val());
        });
});

DEMO

答案 1 :(得分:1)

请从选项标记中删除id(id =&#34; amount-paypal&#34;)属性或使用唯一值。对于Html页面中的每个元素,id应该是唯一的

<select id="selectMembership" class="cat_dropdown " name="selectMembership">
    <option type="text" value="5" selected="selected"  name="amount">5</option>
    <option type="text" value="10"  name="amount">10</option>
    <option type="text" value="20"  name="amount">20</option>    
</select>


THis input needs to receive the selected value    
<input type="text" id="amount-paypal" name="amount">

$(document).ready(function(){

    //choose which price to use from select form
    $('#selectMembership').on('change', function(){

        $('#amount-paypal').val($(this).val());
    });

});

请参阅此链接:http://jsfiddle.net/qwXxm/