Jquery想要检索价值并打印它

时间:2014-05-18 12:20:44

标签: jquery onchange

我想选择更改:将#yourDiscount中的值替换为每个选项的折扣=''

<select name='purchase'>
<option value='100
' discount='95.05'>100
 testów/ 1,045.50 PLN</option>
<option value='500
' discount='567.39' selected>500
 testów/ 4,350.00 PLN</option>
<option value='1000
' discount='1,416.67'>1000
 testów/ 8,500.00 PLN</option>
<option value='2500
' discount='4,000.00'>2500
 testów/ 20,000.00 PLN</option>
<option value='10000' discount='13,846.15'>10000 testów/ 60,000.00 PLN</option>
<option>Inna ilość...</option>
</select>
</div>
<div class='l' style='color:green;margin-top:3px'>&nbsp; <b>i Rabat: <span id='yourDiscount'>567.39</span> PLN</b></div>

任何建议怎么做?

3 个答案:

答案 0 :(得分:2)

$('select').on('change', function() {
    $('#yourDiscount').text($(this).val());
}

答案 1 :(得分:0)

<强> HTML:

<select id="dropPurpose" name='purchase'>
<option value='100
' discount='95.05'>100
 testów/ 1,045.50 PLN</option>
<option value='500
' discount='567.39' selected>500
 testów/ 4,350.00 PLN</option>
<option value='1000
' discount='1,416.67'>1000
 testów/ 8,500.00 PLN</option>
<option value='2500
' discount='4,000.00'>2500
 testów/ 20,000.00 PLN</option>
<option value='10000' discount='13,846.15'>10000 testów/ 60,000.00 PLN</option>
<option>Inna ilość...</option>
</select>
</div>
<div class='l' style='color:green;margin-top:3px'>&nbsp; <b>i Rabat: <span id='yourDiscount'>567.39</span> PLN</b></div>

<强> JS:

$(document).ready( function ()
{
  /* we are assigning change event handler for select box */
    /* it will run when selectbox options are changed */
    $('#dropPurpose').change(function()
    {
        /* setting currently changed option value to option variable */
        var option = $(this).find('option:selected').val();
        /* setting input box value to selected option value */
        $('#yourDiscount').text($(this).val());
    });
});

希望它对你有所帮助。请尝试让我知道。

Demo

答案 2 :(得分:0)

你会在on('change'...事件中获得该选项的attr('discount'),类似这样

  $('select[name="purchase"]').on('change', function(){
   var a = $(this).find('option:selected').attr('discount');
   $('#yourDiscount').text(a);
 });

A fiddle link