[英]Explode checkbox input value in javascript
So i have input checkbox code like this: 所以我输入了这样的复选框代码:
<input type='checkbox' name='cek[]' class='check1' value='$data[id_usulan]' >
And there is an input text with id = test2, so when i click the checkbox, the input text value will change to checkbox value (there's no problem with this), i use this javascript code to do it: 并且有一个id = test2的输入文本,因此当我单击复选框时,输入文本值将更改为复选框值(这没有问题),我使用此javascript代码来做到这一点:
$("input[type=checkbox]").click(function(){
var sum=0;
if ($("input[type=checkbox]").is(':checked')) {
$("input[type=checkbox]:checked").each(function(index){
sum += Number($(this).val().replace( /[^\d.]/g,''));
});
}
$("#test2").val(sum);
} );
And then i change the checkbox: 然后我更改复选框:
<input type='checkbox' name='cek[]' class='check1' value='$data[id_usulan]/$data[jml_harga]' >
As you can see, i tried to use 2 value in one checkbox ( $data[id_usulan]
and $data[jml_harga]
, separated by ' /
' ), but i just want to use one value ( $data[jml_harga]
) for $(this).val()
, well you might think that if i want to use $data[jml_harga]
then i don't have to make 2 value, but don't ask that, there's another reason for why i use 2 value. 如您所见,我试图在一个复选框中使用2个值(
$data[id_usulan]
和$data[jml_harga]
,用' /
'分隔),但我只想使用一个值( $data[jml_harga]
)用于$(this).val()
,那么您可能会认为,如果我想使用$data[jml_harga]
那么我不必取2的值,但是不要问,还有另一个原因为什么我要使用2值。
So this is what I have tried: 所以这就是我尝试过的:
$("input[type=checkbox]").click(function(){
var sum=0;
if ($("input[type=checkbox]").is(':checked')) {
$("input[type=checkbox]:checked").each(function(index){
var explode = $(this).val().split('/');
var price = explode[2];
sum += Number(price.replace( /[^\d.]/g,''));
});
}
$("#test2").val(sum);
} );
But when i click the checkbox, the input text value didn't change to checkbox value and remains the same as before I check, so how to fix this problem? 但是,当我单击复选框时,输入的文本值未更改为复选框值,并且与检查前相同,因此如何解决此问题?
If anyone has a way to solve this, please help me.. :) 如果有人有办法解决这个问题,请帮助我.. :)
Here is the problem: var price = explode[2];
这是问题所在:
var price = explode[2];
The second value is: var price = explode[1];
第二个值是:
var price = explode[1];
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.