简体   繁体   English

如何获取在文本框中输入的值或在javascript中选择的选项的值?

[英]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. 我的问题是如何在javascript或jquery中做到这一点。

Here is my html/php code 这是我的html / php代码

  <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 和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 http://www.thoughtdelimited.org/dirtyFields/examples.cfm

or use predefined value 或使用预定义的值

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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