简体   繁体   中英

jQuery condition if input selected and button clicked

I have the following input list:

 <input type='checkbox' name='182,500' value='182,500'> Below QAR 182,500 <br> <input type='checkbox' name='182,500plus' value='182,500plus'> Above QAR 182,500 – Below QAR 365,000 <br> <input type='checkbox' name='365,000' value='365,000'> Above QAR 365,000 <span class="custom-link btn border-width-0 info-submit btn-color-xsdn btn-outline btn-icon-left">Submit</span>

I want to create an if statement where if the input with the name attribute 182,500 is checked and if the span with the class .info-submit is clicked, a snippet of code is executed.

Maybe something like that:

if ($("input[name = '182,500']").attr('checked') && $(".section-2-1 .info-submit").click() ) {
  // execute code
} 

.on('click') method you can check if the input is checked or not using prop

 $('.info-submit').on('click', function() { $('[name="182,500"]').prop('checked') ? console.log('clicked and checked') : console.log('clicked only') })
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <input type='checkbox' name='182,500' value='182,500'> Below QAR 182,500 <br> <input type='checkbox' name='182,500plus' value='182,500plus'> Above QAR 182,500 – Below QAR 365,000 <br> <input type='checkbox' name='365,000' value='365,000'> Above QAR 365,000 <br /> <span class="custom-link btn border-width-0 info-submit btn-color-xsdn btn-outline btn-icon-left">Submit</span>

So what you need to do is check that the input with the required name is checked when the info-submit button is clicked. For this case, you can use .is(':checked') on the input. It will give you true or false depending on it's state.

$('.info-submit').on('click', function() {
    // This will check if the input is checked or not.
    if($("input[name='182,500']").is(':checked')) {
        // Do something.
    }
})

Hope it helps!

$('.info-submit').on('click', function () {
    // This will check if the input is checked or not.
    if ($("input[name='182.500']").prop('checked')) {
        // Do something.
    }
})

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM