2016-05-19 2 views
0

У меня есть следующая структура html и вы хотите изменить значение атрибута цены при заполнении другого ввода.Как изменить значение атрибута ввода с помощью jquery

<dd> 
    <div class="input-box additional-images imprint-images" style="height: 400px; padding-bottom: 0px; outline: medium none; overflow: hidden; width: 889px; padding-right: 0px;" tabindex="0"> 
     <ul id="options-788-list" class="options-list"> 
      <li> 
       <input type="checkbox" price="0" value="9697" id="options_788_2" name="options[788][]" onclick="opConfig.reloadPrice()" class="checkbox product-custom-option"> 
      </li> 
     </ul> 
    </div> 
</dd> 
    <div class="color-quantity not-selected-inputs"> 
     <input type="text" name="custom" onkeydown="return myFunction(event);"> 
    </div> 

Я попытался изменить атрибут цен, используя следующий сценарий, но не работает:

var quantitys = 0; 
jQuery(".color-quantity > input").each(function() { 
    if (this.value) { 
     quantitys += (this.value) * 1; 
    } 

    if (parseInt(quantitys) >= "25") { 
     alert("more than 25"); 
     jQuery("dd > .imprint-images > ul > li > input").attr("price", "5.00"); 
    } 
}); 

Пожалуйста, помогите мне в этом случае.

+2

1) 'price' не является допустимым атрибутом для' input' элемента, использовать '' данных- * Атрибут 2) '(ParseInt (quantitys)> = "25") 'вы забыли radix, и вы сравниваете int с строкой –

+0

где элемент .color-quantity> input html? Я не вижу его в вашем html? –

+0

@Rory McCrossan взгляните на это: jQuery ("dd> .imprint-images> ul> li> input"). Attr ("price")). ToFixed (2). ("Цена", "5.00 «); – Xabby

ответ

0

HTML

<dd> 
    <div class="input-box additional-images imprint-images" style="height: 400px; padding-bottom: 0px; outline: medium none; overflow: hidden; width: 889px; padding-right: 0px;" tabindex="0"> 
     <ul id="options-788-list" class="options-list"> 
      <li> 
       <input type="checkbox" data-price="0" value="9697" id="options_788_2" name="options[788][]" onclick="opConfig.reloadPrice()" class="checkbox product-custom-option"> 
      </li> 
     </ul> 
    </div> 
</dd> 
    <div class="color-quantity not-selected-inputs"> 
     <input type="text" name="custom" onkeydown="return myFunction(event);"> 
    </div> 

JS

var quantitys = 0; 
jQuery(".color-quantity > input").each(function() { 
    if (this.value) { 
     quantitys += parseInt(this.value); 
    } 

    if (quantitys >= 25) { 
     alert("more than 25"); 
     $("div.imprint-images > ul > li > input").attr("data-price", "5.00"); 
    } 
}); 

Будет ли эта работа для вас?

+0

Фактически цена att динамически создается из Magneto CMS не жестко закодирован сам, так что я могу его изменить – Xabby

0

проверить это я изменил цены к данным цена на вашем флажок и добавить манекен классу update_price_element

добавить класс add_price в текстовое поле

HPE это помогает.

var quantitys = 0; 
 
\t $('.add_price').blur(function(){ 
 
\t \t var value = $(this).val(); 
 
\t \t if (value) { 
 
\t \t \t quantitys += (this.value) * 1; 
 
\t \t } 
 
\t \t if (parseInt(quantitys) >= "25") { 
 
\t \t \t alert("more than 25"); 
 
\t \t \t $(".update_price_element").attr("data-price", "5.00"); 
 
\t \t } 
 
\t });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script> 
 
<dd> 
 
    <div class="input-box additional-images imprint-images" style="height: 400px; padding-bottom: 0px; outline: medium none; overflow: hidden; width: 889px; padding-right: 0px;" tabindex="0"> 
 
     <ul id="options-788-list" class="options-list"> 
 
      <li> 
 
       <input type="checkbox" data-price="0" value="9697" id="options_788_2" name="options[788][]" onclick="opConfig.reloadPrice()" class="checkbox product-custom-option update_price_element"> 
 
      </li> 
 
     </ul> 
 
    </div> 
 
</dd> 
 
<div class="color-quantity not-selected-inputs"> 
 
<input type="text" name="custom" class="add_price" onkeydown="" /> 
 
</div>

+0

На самом деле цена att динамически создается из Magneto CMS, а не жестко закодирована мной, поэтому я могу ее изменить – Xabby

+0

не проблема, сохраните эту цену как data-attr или переменную. затем добавьте туда. –

+0

Основная проблема заключается в том, что я не могу получить доступ к цене attr – Xabby

Смежные вопросы