[英]How to retrive a $scope value outside the function it is defined, in Angular JS
[英]Retrive value from array id in js function
嗨,我正在編寫一個基於PHP的代碼,其中我在生成具有不同ID和名稱的復選框
<input type="checkbox" name="settle_check[]" id="settle_check['.$res2['id'].']" value="1" onclick="calculate_discount(\''.$res2['id'].'\');"/>
我計算折扣的功能如下
function calculate_discount(id){
alert($('#settle_check['+id+']').val());
if($('#settle_check['+id+']').is(":checked")){
alert('Hiii');
}
else{
alert('Byeee');
}
}
現在,它每次都沒有捕獲值並發出未定義的警報。 請幫我。
您正在ID選擇器中使用[]
作為元字符。 您需要逃脫它們。
采用
$('#settle_check\\['+id+'\\]').val()
要將任何元字符(例如!“#$%&'()* +,。/ :; <=>?@ [] ^`{|}〜)用作名稱的文字部分,必須以兩個反斜杠轉義:\\\\。
要么
$('[id="settle_check[' + id +']"]').val()
您也可以嘗試以下操作:
而不是傳遞id,而是像這樣傳遞元素本身:
<input type="checkbox" name="settle_check[]" value="1" click="calculate_discount(this);"/>
並將功能更新為:
function calculate_discount(element){
alert($(element).val());
if($(element).is(":checked")){
alert('Hiii');
}
else{
alert('Byeee');
}
}
使用此解決方案,您可以避免不必要的jQuery搜索。
用這個
$('#settle_check\\['+id+'\\]').val()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.