根据之前选择的值更改选择的值

时间:2015-09-21 09:49:03

标签: javascript jquery forms

我在这个网站和其他网站上都尝试了这个,然后才问这个,但是没有人解决问题

我有这个表格,带有选择选项

<select name="stampa_front">
    <option data-price="0">nessun colore</option> 
    <option data-price="0.80" data-label="1 colore stampa fronte"> 1 colore </option>
    <option data-price="0.90" data-label="2 colore stampa fronte"> 2 colori  </option>
    <option data-price="1.00" data-label="3 colore stampa fronte"> 3 colori </option>
</select >
<select name="stampa_retro">
<option data-price="0">nessun colore</option> 
    <option data-price="0.80" data-label="1 colore stampa retro"> 1 colore </option>
    <option data-price="0.90" data-label="2 colore stampa retro"> 2 colori  </option>
    <option data-price="1.00" data-label="3 colore stampa retro"> 3 colori </option>
</select>
<select name="stampa_sinistra">
    <option data-price="0">nessun colore</option> 
    <option data-price="0.80" data-label="1 colore stampa sinstra"> 1 colore </option>
    <option data-price="0.90" data-label="2 colore stampa sinistra"> 2 colori  </option>
    <option data-price="1.00" data-label="3 colore stampa sinistra"> 3 colori </option>
</select >

<select name="stampa_destra">
    <option data-price="0">nessun colore</option> 
    <option data-price="0.80" data-label="1 colore stampa destra"> 1 colore </option>
    <option data-price="0.90" data-label="2 colore stampa destra"> 2 colori  </option>
    <option data-price="1.00" data-label="3 colore stampa destra"> 3 colori </option>
</select>

我希望如果&#39;选择了&#34;以外的选项; nessun colore&#34;关于&#34; 1 colore&#34;的价格选项改变价值 例如,如果&#34; stampa fronte&#34;在下一个选择一种颜色时选择值为0.80,而如果未选择该值则为1:00

我希望你理解我..

1 个答案:

答案 0 :(得分:0)

我无法得到您正在尝试做的事情,但您可以使用以下内容作为开头:

# this for the first select, selected by name and when change it will 
# handled by the callback, (this) mean the select which is the callback is for
$("select[name='stampa_front']").on('change', function() {
  if($(this).val() != '0'){
    # do somethinge here if someone changed the first select and choose any option which is not the first
    # what will do here is: it will select the same value selected from the first select
    $("select[name='stampa_front']").val($(this).val());
  }else{
    # do something here different if the user select first option "0"
  }
});