I have the following code:
$("input[id^='Order_'], input[id^='Default_']")
.change(function (e) {
var type = $(this).attr('id').split('_')[0];
updateField('Menu', $(this), type);
});
The input with an id of 'Order_' has a type="text"
The input with an id of 'Default_' has a type="checkbox"
In my updateField I have a problem:
function updateField(entity, obj, type) {
var val = obj.val();
var idArr = obj.attr("id");
var idTmp = idArr.split("_");
var id = idTmp[1];
var pkrk = $("span[id='refKey_" + id + "']").html();
The problem is that the code works good for the text but not for the checkbox. If the checkbox is checked or not then it still returns true. Is there a way that I can get the value of if the checkbox is checked or not and either put the string “true” or “false” into the variable val while still having the correct value put into val if the date is coming from the Order_ ?
So if the value of Order_ field is 25 then val = "25"
So if the value of Default_ checkbox is true the val = "true"
So if the value of Default_ checkbox is false the val = "false"
The problem is that the checkbox doesnt have any “value”. Instead of using the
val()function in jQuery search for the attribute “checked”.This can be done as such:
Or using the jQuery selectors
So the
attr()function will return undefined if the checkbox tag doesnt contain the attribute “checked” and that is the same as the unchecked state of the checkbox.