简体   繁体   中英

Validate a select box value in jquery

I have a select box inside a popup. If the first value is selected , i need to call another popup. iam using jQuery impromptu popup and it has 'html' and 'submit' functions to add HTML & the submit callback respectively. Is there any way i can validate the select box value inside the popup before submitting.

var my_states = {
state0: {
    title: 'Name',
    html:'<label>First <input type="text" name="fname" value=""></label><br />'+
        '<label>Last <input type="text" name="lname" value=""></label><br />',
    buttons: { Next: 1 },
    //focus: "input[name='fname']",
    submit:function(e,v,m,f){ 
        console.log(f);

        e.preventDefault();
        $.prompt.goToState('state1');
    }
},
state1: {
    title: 'Check Here',
    html:'<table><tr><td><strong>Generate ODEC ID </strong></td><td>:</td><td id=\'gen_odec\' class=\'gen_odec\'>   <select name=\'select_odec\' id=\'select_odec\' class=\'select_odec\'>'+<?php echo json_encode($odecpref)?>+'</select></td></tr></table>',
    buttons: { Back: -1, Next: 1 },
    //focus: ":input:first",
    submit:function(e,v,m,f){ 
        console.log(f);

        if(v==1) {$.prompt.close();
                    return false;}
        if(v==-1) $.prompt.goToState('state0');
        e.preventDefault();
    }
},  
 };  
 $.prompt(my_states);

EDIT 1 : Insid the 'html' & inside the of the select box I tried this also, with a button attached, but i think iam missing something-

<input id="clickMe" type="button" value="clickme" onclick="$.prompt(\"Hello!\");" />

also,

<input id="clickMe" type="button" value="clickme" onclick="doFunc()" />

EDIT 2 Tried this ,also but error says: Uncaught SyntaxError: Unexpected token }

<td id=\'gen\' class=\'gen_odec\'>  <select name=\'select_odec\' id=\'select_me\' class=\'select_me\'>'+<?php echo json_encode($odecpref)?>+'</select><input id="clickMe" type="button" value="clickme" onclick="doFunction();function doFunction(){$.prompt("Hello!");}" /></td>

As far as I can understand you need to add listener to change event on your select . But since that select added to document dynamically you should use event delegation . Then, in the handler, you check the select value and just call you destination pop-up. Something like that:

$(document).on('change', '#select_odec', function(){
    if($(this).val() === 'valueOfDesiredOption') {
        $.prompt.goToState('stateYouNeed');
    }
});

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