[英]Update input field with jQuery when .text(); changes
我正在將jQuery定價計算器與購物車集成在一起。 我想在.text();時更新一個隱藏字段。 元素的值已更改。
這是我要更新的字段:
<input id="firewall-price" type="hidden" name="price" value="" />
從這里:
<div class="orderRow server firewall" contain="firewall" id="">
<div class="quantity"><span></span></div>
<div class="name">Firewall</div>
<div class="price orderPrice">$0</div>
</div>
使用這個jQuery:
$(document).ready(function(){
var price = $( ".firewall .price" ).text().substr(1);
$(price).change(function(){
$("input[name=firewall-price]").val(price);
});
});
更改數字后,我無法獲取輸入[name = firewall-price]的值進行更新。 我知道這樣做有點奇怪,但是我只想從計算器中獲取最終值並將其發送到購物車。
這是開發站點的鏈接: http : //mindcentric.thelabelcreative.com/pricing_calculator/
您不能在div上放置change事件,更不用說字符串了。 您所描述的是MutationEvent,被認為是不好的做法(不再受支持)。 您可以在這里閱讀有關內容: https : //developer.mozilla.org/en-US/docs/Web/Guide/Events/Mutation_events
相反,您需要了解更改該div值的事件/操作。
嘗試更換:
$("input[name=firewall-price]").val(price);
同
$("#firewall-price").val(price);
胡安的回答是正確的,我只想讓你知道為什么。
<input id="firewall-price" type="hidden" name="price" value="" />
沒有name屬性,這是您要求jQuery選擇元素的依據。
要在不更改jQuery調用的情況下使代碼工作,只需將name屬性添加到輸入字段即可。 在html中,當元素提交到服務器時,name屬性用於關聯鍵/值對。
對於div上的更改事件,Sahbeewah是正確的。 如果您能夠編輯main.js文件的內容,建議您在lastPrice函數中添加邏輯。 在第451行之后添加
$('#firewall-price').val(correctPrice);
要么
$('#firewall-price').val(str);
correctPrice是未格式化的,str帶有美元符號和逗號。
正如一些答案所說,您不能將更改事件綁定到文本值。
但是,您可以將更改事件綁定到輸入,因此您需要反轉邏輯,而不是更新$( ".firewall .price" ).text()
而是更新輸入,將更改事件綁定到輸入$('#firewall-price')
並.price
設置.price
元素的text()
。
$(document).ready(function(){
var $priceTextElement = $( ".firewall .price" );
var $priceTextInput = $('#firewall-price');
$priceTextInput.on('change', function(){
$priceTextElement.text('$'+$priceTextInput.val());
});
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.