![](/img/trans.png)
[英]How to check the input checkbox field with a value on page load using jquery
[英]Check checkbox and set value on load content/page
我有一家商店。 在購物車中(通常用戶不登錄時)是設置發票的復選框。 如果未選中-用戶將收到帳單。
下一步,用戶必須登錄,然后轉到“已處理”頁面。 當用戶繼續操作(登錄頁面之后)時,如果不返回購物車,則無法更改復選框。
我需要檢查(登錄后,在操作頁面上)“客戶是公司”還是普通用戶(“私人”),如果用戶是“公司”,請將發票設置為默認值。
我有那個代碼:(我知道不是純PHP的-是某種框架)
<!-- CART PAGE with checkbox -->
<div id="additionalRealizationInfo">
?{foreach system.cart.optionsData option}
<label>
<input class="additionalRealizationInfoCheckbox"
type="checkbox"
?{if true == option.option.enabled}
checked="checked"
?{endif}
name="additional?{=system.randomNumber}"
value="${system.controllerUrl}ToggleOption/?{=option.name}" />
?{=option.title}
</label>
?{endforeach}
// The Value of checkbox is: toggleOption/facture
<!-- PROCCED PAGE without checkbox - On this page, after login, user see own choice from cart before login -->
<div id="additionalRealizationInfo">
<h2>Infos</h2>
<div>
?{foreach system.cart.optionsData option}
<div>
?{if true == option.option.enabled}
You get Invoice
?{else}
You get recive
?{endif}
</div>
?{endforeach}
</div>
</div>
<script type="text/javascript">
var isCompany = "?{=system.userLogged.InvoiceData.company}"; <!-- Company Name -->
var enableInvoice = "?{=system.cart.invoiceEnabled}";
if (isCompany.lenght > 0) {
$.get("${system.controllerUrl}setInvoiceEnabled",function(resp) {
$.get("${system.baseUrl}view/enableinvoice",function(resp) {
$("#additionalRealizationInfo").html(resp);
});
});
$.get("${system.controllerUrl}toggleOption/facture",function(resp) {
});
};
</script>
// string to check choice: ?{if system.cart.invoiceEnabled}
// system.baseURL / view / enableinvoice是要顯示的一些代碼,該發票已設置為默認值(腳本有效時)
怎么了。 沒用
乍一看,我將檢查以下代碼行:
if (isCompany.lenght > 0) {
您拼錯了長度。 嘗試這個:
if (isCompany.length > 0) {
或者,取決於“ isCompany”變量的值是true / false,您可以將其寫為
if (isCompany) { // check boolean value instead
希望有幫助!
我更改了腳本並且它可以工作,但是只有在我重新加載/刷新頁面之后才可以。
<!-- CART PAGE before and after login -->
var companyCustomer = "?{=system.userLogged.InvoiceData.company}";
//If user is logged, companyCustomer has a value. If not = ""
var enableInvoice = "?{=system.cart.invoiceEnabled}";
// If true = "1" , If false = "". That's not a boolean string.
var klik = $("#additionalRealizationInfo>label:first-child>input:checkbox");
if (companyCustomer) {
klik.prop('checked', true);
klik.prop("disabled", true);
klik.css('cursor','default');
$.get("${system.controllerUrl}toggleOption/facture",function(resp){
console.log(resp);
});
}; // End if (companyCustomer)
<!-- PROCEED PAGE after login -->
var companyCustomer = "?{=system.userLogged.InvoiceData.company}";
//If user is logged, companyCustomer has a value. If not = ""
var enableInvoice = "?{=system.cart.invoiceEnabled}";
// If true = "1" , If false = "". That's not a boolean string.
if (companyCustomer) {
if (enableInvoice) {
} else {
$.get("${system.controllerUrl}toggleOption/facture",function(resp){
console.log(resp);
});
}; // end if (enableInvoice)
}; // end if (companyCustomer)
如何在加載時執行,以及如何動態更改/設置此切換選項? 當我沒有選中CART PAGE中的復選框時,轉到登錄旁邊,然后自動轉到我得到的PROCEED PAGE
enableInvoice = "";
我嘗試了.ready
和.load()
和.live()
,但沒有任何效果。 嘗試通過.click
執行,但我不希望使用此方法。 內容顯示后,我也不想刷新頁面。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.