Page 1 of 1
CheckBox Value
Posted: Wed Dec 07, 2022 11:38 am
by mishanian
Hi,
What is the best way to get the value of a checkbox by the name of "BatchFailed"?
I tried following codes based on JQuery suggestion but all of them return undefined
$("input[name='x_BatchFailed[]']:checked").val()
----------
$("input[name='x_BatchFailed']:checked").val()
---------
$("input[name='BatchFailed[]']:checked").val()
--------
$("input[name='BatchFailed']:checked").val()
--------
$("input[name='x_BatchFailed[]']").prop( "checked" )
----------
$("input[name='x_BatchFailed']").prop( "checked" )
---------
$("input[name='BatchFailed[]']").prop( "checked" )
--------
$("input[name='BatchFailed']").prop( "checked" )
Re: CheckBox Value
Posted: Wed Dec 07, 2022 11:55 am
by mobhar
What is the type of the field? You may post your table schema (CREATE TABLE ...
) including some records example (INSERT INTO ...
) so others could comprehend the situation and try straightforward.
Re: CheckBox Value
Posted: Wed Dec 07, 2022 11:57 am
by arbei
You should right click the element in your browser and inspect the HTML to find out the correct CSS selector, see Inspect HTML element and add your own CSS styles.
Re: CheckBox Value
Posted: Wed Dec 07, 2022 12:12 pm
by mishanian
Hi, thanks. I can find if it is checked or not with the following command:
$("#x_BatchFailed").prop("checked")
but I can not get the proper value.
$("#x_BatchFailed").val()
always return '1' if it is checked or unchecked
CREATE TABLE test
(
id
int(2) NOT NULL AUTO_INCREMENT,
BatchFailed
tinyint(1) DEFAULT NULL,
PRIMARY KEY (id
)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
and it is a checkbox in the PHPMaker with Value=1 Lable=Yes and Value=0 Lable No
Re: CheckBox Value
Posted: Wed Dec 07, 2022 12:15 pm
by arbei
mishanian wrote:
$("#x_BatchFailed").val()
always return '1' if it is checked or unchecked
You may read :checked Selector.
Re: CheckBox Value
Posted: Wed Dec 07, 2022 12:24 pm
by mishanian
Thanks a lot, it is working with your suggestion as the following command:
$("#x_isFailed:checked").val()
I have a similar question for Radio Button. I will write it in a separate reply
Re: CheckBox Value
Posted: Wed Dec 07, 2022 12:28 pm
by mishanian
Hi again, to get the radio button "MyRadio" for the following sample table:
CREATE TABLE test
(
id
int(2) NOT NULL AUTO_INCREMENT,
isFailed
tinyint(1) DEFAULT NULL,
MyRadio
int(1) DEFAULT NULL,
PRIMARY KEY (id
)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
it is user defined with the following value, labels: value:1 label:One , value:2 label: two
when I used
$("#x_MyRadio:checked").val()
it returned undefined. I believe $("#x_MyRadio:checked")
is a correct JQuery selector.
please correct me.
Re: CheckBox Value
Posted: Wed Dec 07, 2022 12:33 pm
by mishanian
for your information, this is source code
<div id="r_MyRadio" class="row">
<label id="elh_test_MyRadio" class="col-sm-2 col-form-label ew-label">My Radio</label>
<div class="col-sm-10"><div>
<span id="el_test_MyRadio">
<template id="tp_x_MyRadio">
<div class="form-check">
<input type="radio" class="form-check-input" data-table="test" data-field="x_MyRadio" name="x_MyRadio" id="x_MyRadio">
<label class="form-check-label"></label>
</div>
</template>
<div id="dsl_x_MyRadio" class="ew-item-list"></div>
<selection-list hidden
id="x_MyRadio"
name="x_MyRadio"
value=""
data-type="select-one"
data-template="tp_x_MyRadio"
data-target="dsl_x_MyRadio"
data-repeatcolumn="5"
class="form-control"
data-table="test"
data-field="x_MyRadio"
data-value-separator=", "
></selection-list>
<div class="invalid-feedback">Incorrect integer - My Radio</div>
</span>
</div></div>
</div>
Re: CheckBox Value
Posted: Wed Dec 07, 2022 12:48 pm
by arbei
mishanian wrote:
I believe $("#x_MyRadio:checked")
is a correct JQuery selector.
It is incorrect. The "#x_MyRadio" selector only select the first element with that id, you need to select all the radio buttons with ":check" for that field.
arbei wrote:
Re: CheckBox Value
Posted: Wed Dec 07, 2022 12:53 pm
by mobhar
You may try this in Startup Script of Add/Edit Page:
$('#x_MyRadio').change(function(){
ew.alert("Selected Radio Button value: " + $(this).attr("value"));
});
Re: CheckBox Value
Posted: Wed Dec 07, 2022 12:59 pm
by mobhar
If you want to get the selected value, for example in Edit Page, then simply try this in Startup Script, too:
ew.alert("Radio Button selected: " + $('#x_MyRadio').val());
Re: CheckBox Value
Posted: Wed Dec 07, 2022 1:09 pm
by mishanian
I really appreciate your answers:
works like a charm.
thanks
Re: CheckBox Value
Posted: Wed Dec 07, 2022 3:02 pm
by arbei
arbei wrote:
The "#x_MyRadio" selector only select the first element with that id, you need to select all the radio buttons with ":checked" for that field.