admin管理员组

文章数量:1122832

I am tring to get the state of the checkbox from an html table in javascript so I can then use it to edit information on the following page, and then save to mysql database.

When I click on the edit button it runs the following script and then go to the edit page where I need a 1 or 0 depending on the checkbox state to carry over, but it does not or at least in the format I need.

Using javascript Alert(); it displays the entire html input line code with checked='checked' at the end. When I click the edit button on a record in the html table that has the checkbox set in the database.

How can I get what I need?

here is the script code.

<script>
function editsubcategory(element, value) {
    var _$index = (element.parentNode.parentNode.rowIndex)
    var myTab = document.getElementById('subcategorytable');
    var objCells = myTab.rows.item(element.parentNode.parentNode.rowIndex).cells;
    var _$subCategoryID = objCells.item(0).innerHTML; // categoryID
    var _$subCategoryName = objCells.item(1).innerHTML; // categoryName
    var _$subCategoryDescription = objCells.item(2).innerHTML; // categoryDescription
    var _$subCategoryMain = objCells.item(3).innerHTML; // subCategoryMain
    var _$subCategoryType = objCells.item(4).innerHTML; // subCategoryType
    var _$subCategoryBudget = objCells.item(5).innerHTML; // subCategoryType
    var _$subCategoryDiscretionary = objCells.item(6).innerHTML; // subCategoryType


    var _$btn = value; // button

    // alert(_$subCategoryID);
    // alert(_$subCategoryName);
    // alert(_$subCategoryDescription);
    // alert(_$subCategoryMain);
    // alert(_$subCategoryType);
    // alert(_$subCategoryBudget);
    // alert(_$subCategoryAccount);
    alert(_$subCategoryDiscretionary);
    // alert(_$btn);

    window.location.href = "edit-subcategory.php?subCategoryID=" + _$subCategoryID
+ "&btn=" + _$btn
+ "&subCategoryName=" + _$subCategoryName
+ "&subCategoryDescription=" + _$subCategoryDescription
+ "&subCategoryMain=" + _$subCategoryMain
+ "&subCategoryType=" + _$subCategoryType
+ "&subCategoryBudget=" + _$subCategoryBudget
+ "&subCategoryDiscretionary=" + _$subCategoryDiscretionary;

};
</script>

I have tried different ways to get the state, but nothing has worked. I think the issue is with what is being returned from the var _$subCategoryDiscretionary = objCells.item(6).innerHTML; line but I have no idea how to get the state.

This 6th item contains a checkbox.

I just want the variable _$subCategoryDiscretionary to be either a 1 or 0, true or false, checked or unchecked so i can perform an IF statement on it.

I am tring to get the state of the checkbox from an html table in javascript so I can then use it to edit information on the following page, and then save to mysql database.

When I click on the edit button it runs the following script and then go to the edit page where I need a 1 or 0 depending on the checkbox state to carry over, but it does not or at least in the format I need.

Using javascript Alert(); it displays the entire html input line code with checked='checked' at the end. When I click the edit button on a record in the html table that has the checkbox set in the database.

How can I get what I need?

here is the script code.

<script>
function editsubcategory(element, value) {
    var _$index = (element.parentNode.parentNode.rowIndex)
    var myTab = document.getElementById('subcategorytable');
    var objCells = myTab.rows.item(element.parentNode.parentNode.rowIndex).cells;
    var _$subCategoryID = objCells.item(0).innerHTML; // categoryID
    var _$subCategoryName = objCells.item(1).innerHTML; // categoryName
    var _$subCategoryDescription = objCells.item(2).innerHTML; // categoryDescription
    var _$subCategoryMain = objCells.item(3).innerHTML; // subCategoryMain
    var _$subCategoryType = objCells.item(4).innerHTML; // subCategoryType
    var _$subCategoryBudget = objCells.item(5).innerHTML; // subCategoryType
    var _$subCategoryDiscretionary = objCells.item(6).innerHTML; // subCategoryType


    var _$btn = value; // button

    // alert(_$subCategoryID);
    // alert(_$subCategoryName);
    // alert(_$subCategoryDescription);
    // alert(_$subCategoryMain);
    // alert(_$subCategoryType);
    // alert(_$subCategoryBudget);
    // alert(_$subCategoryAccount);
    alert(_$subCategoryDiscretionary);
    // alert(_$btn);

    window.location.href = "edit-subcategory.php?subCategoryID=" + _$subCategoryID
+ "&btn=" + _$btn
+ "&subCategoryName=" + _$subCategoryName
+ "&subCategoryDescription=" + _$subCategoryDescription
+ "&subCategoryMain=" + _$subCategoryMain
+ "&subCategoryType=" + _$subCategoryType
+ "&subCategoryBudget=" + _$subCategoryBudget
+ "&subCategoryDiscretionary=" + _$subCategoryDiscretionary;

};
</script>

I have tried different ways to get the state, but nothing has worked. I think the issue is with what is being returned from the var _$subCategoryDiscretionary = objCells.item(6).innerHTML; line but I have no idea how to get the state.

This 6th item contains a checkbox.

I just want the variable _$subCategoryDiscretionary to be either a 1 or 0, true or false, checked or unchecked so i can perform an IF statement on it.

Share Improve this question edited Nov 22, 2024 at 2:01 Shadow 34.2k10 gold badges63 silver badges72 bronze badges asked Nov 22, 2024 at 1:40 newtowebappnewtowebapp 14 bronze badges 3
  • That mess looks like it just parses the href value for <a>. Post what the rendered HTML looks like for a row <tr><td>...</td><td>...,...</tr>. – zer00ne Commented Nov 22, 2024 at 2:31
  • 2 I guess that objCells.item(6) is a td. Try objCells.items(6).querySelector("input[type='checkbox']").checked – James Commented Nov 22, 2024 at 4:25
  • It would help us to help you if you edit your question as a minimal reproducible example; this script relies on html which you have no provided. – mykaf Commented Nov 22, 2024 at 15:01
Add a comment  | 

1 Answer 1

Reset to default 0

If you want the checked value and not the html, then get the .checked value not the .innerHTML

var _$subCategoryDiscretionary = objCells.item(6).checked;

本文标签: javascriptHow to get TRUEFALSE or CHECKEDUNCHECKED or 10 from html checkboxStack Overflow