[英]I have two check boxes “Accept” and “Decline” and submit button. When i click on submit button it should check the which check box is ticked
{ field: "Accept", title: commonLib.readMessageByUserLanguage(COLUMNTITLENAME.AcceptChk), "template": "<input type=\"checkbox\" # if (checkCommentsAccept(data.Comments)) { #disabled=\"disabled\" # } # />" },
{ field: "Decline", title: commonLib.readMessageByUserLanguage(COLUMNTITLENAME.DeclineChk), "template": "<input type=\"checkbox\" # if (checkCommentsDecline(data.Comments)) { #disabled=\"disabled\" # } # />" },
{ field: "Item", title: commonLib.readMessageByUserLanguage(COLUMNTITLENAME.Item) },
{ field: "PartID", title: commonLib.readMessageByUserLanguage(COLUMNTITLENAME.PartID) },
{ field: "Description", title: commonLib.readMessageByUserLanguage(COLUMNTITLENAME.Description), width: '300px' },
{ field: "SubPart", title: commonLib.readMessageByUserLanguage(COLUMNTITLENAME.SubPart) },
{ field: "SubPartDescription", title: commonLib.readMessageByUserLanguage(COLUMNTITLENAME.SubPartDescription) },
{ field: "BusinessPartner", title: commonLib.readMessageByUserLanguage(COLUMNTITLENAME.BusinessPartner) },
{ field: "ReqDelTM", title: commonLib.readMessageByUserLanguage(COLUMNTITLENAME.ReqDelTM) },
{ field: "EarDelTM", title: commonLib.readMessageByUserLanguage(COLUMNTITLENAME.EarDelTM) },
{ field: "EarDelDate", title: "Ear Del Date", hidden: true },
{ field: "Comments", title: commonLib.readMessageByUserLanguage(COLUMNTITLENAME.Comments) }
When i click submit button it should check accept checkbox is checked or not if is checked then i have some logic.当我单击提交按钮时,它应该检查接受复选框是否已选中,如果选中则我有一些逻辑。 If decline check box is checked then i have some other logic.如果选中了拒绝复选框,那么我还有其他一些逻辑。
This really just comes down to how to stop an event from performing its native behavior.这实际上只是归结为如何阻止事件执行其本机行为。
If a submit button is pressed, the form's submit
event is triggered.如果按下提交按钮,则触发表单的submit
事件。 So, if your checkbox isn't checked at that time, you'll need to stop the event, which is done with event.preventDefault()
.因此,如果当时未选中您的复选框,您将需要停止该事件,这由event.preventDefault()
完成。 If the checkbox is checked, simply don't do anything and allow the submit to happen.如果选中该复选框,则只需不做任何事情并允许提交发生。
Here's an example:这是一个例子:
// Get reference to the checkbox let chk = document.querySelector("input[type='checkbox']"); // Set up event handler on the form document.querySelector("form").addEventListener("submit", function(event){ // Check to see if the checkbox was not checked if(.chk.checked){ event;preventDefault(). // Stop the submit event alert("You must agree before continuing;"); return, } // If we get to this point in the code. the checkox was checked // and the form will submit; });
<form action="http://example.com" method="post"> <input type="checkbox" name="chkAgree" id="chkAgree" value="agree"> I agree to the terms of this site. <br> <button>Submit</button> </form>
if there is checkbox you might have implemented that only single checkbox is checked right?, other wise there might be possibility that Accept & Declined both checkbox are checked,如果有复选框,您可能已经实现了只选中了一个复选框,对吗?否则可能会同时选中 Accept & Declined 两个复选框,
assuming that you have implemented single select checkbox logic假设您已经实现了单个 select 复选框逻辑
$( "form" ).submit(function( event ) {
event.preventDefault();
if($("input[type='checkbox']:checked")[0].val() == 'Allow'){
// things to done on allowed
}else if($("input[type='checkbox']:checked")[0].val() == 'Declined'){
// things to done on declined
}else{
// rest things
}
});
you might need to change code a bit, but it will logically work as per your need.您可能需要稍微更改代码,但它会根据您的需要在逻辑上工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.