简体   繁体   中英

How to get the value of either entered value in textbox OR selected option in javascript?

I have a popup that prompts the user to make a selection from dropdown menu or if is not in the DB there is a textbox for user to enter a number.

I would like to grab the value of whatever the user picked to do ie either choice (1 of the 2 not both ) and do something with the value. My problem is how to do that in javascript or jquery.

Here is my html/php code

  <table>
    <tr>
    <td class='Fnumber'> <span><?php xl('Send to', 'e'); ?>:</span></td>
    </tr>
    <tr>
    <td>
            <select id='numberinfo' name='send_to'>
            <?php
            echo "<option value='' selected='selected'>Select Number Destination</option>";
            foreach($send_to as $key => $value) {
                    echo "  <option value='$value' ";
                    echo ">$value </option>\n";
             }
            ?>
            </select>
    </td>
    </tr>
    <tr>
    <td class='Fnumber'><span><?php echo "OR" ?></span></td>
    </tr>
    <tr>
    <td class='Fnumber'><span><?php xl('Enter Your Number', e); ?>:</span></td><br/>
    <tr>
    <td class='Fnumber'>
    <input type="text" class="clearable" name="send_to" placeholder="Enter Your Number" maxlength="10" onkeypress="return isNumberKey(event)"/>
    </td>
    </tr>

    </table>

will this logic do? Assume

<select id='numberinfo' name='send_to'>
   <option value='initialvalue' selected="selected">Select One</option>
   <option value='value1'>Select One</option>
   <option value='value2'>Select One</option>
</select>
<input id="inputtext" type="text" placeholder="Input Number"/>

and javascript

var finalvalue = "";
if ($("#numberinfo").val() == 'initialvalue') {
    if ($("#inputtext").val().length > 0) {
        finalvalue = $("#inputtext").val();
    }
} else {
    finalvalue = $("#numberinfo").val();
}
if (finalvalue.length == 0) {
    alert("Please input one of two")
} else {
    alert("Inputed: " + finalvalue)
}

you can check wether the input changed or not

How do I know that a form input has changed?

http://www.thoughtdelimited.org/dirtyFields/examples.cfm

or use predefined value

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