我是JavaScript和jQuery的新手。我想显示一个combobox-A,这是一个HTML <选择>与其选择的id和内容在onChange()的其他地方。

如何通过其选择id完整的组合框,以及如何通过onChange事件的其他参数?


当前回答

这段代码一旦我写只是解释onChange事件的选择,你可以将这段代码保存为html,并看到输出它的工作。对你们来说很容易理解。

<html>
    <head>
        <title>Register</title>
    </head>
    <body>
    <script>
        function show(){
            var option = document.getElementById("category").value;
            if(option == "Student")
                  {
                        document.getElementById("enroll1").style.display="block";
                  }
            if(option == "Parents")
                  {
                        document.getElementById("enroll1").style.display="none";
                  }
            if(option == "Guardians")
                  {
                        document.getElementById("enroll1").style.display="none";
                  }
        }
    </script>
            <form action="#" method="post">
                <table>
                    <tr>
                        <td><label>Name </label></td>
                        <td><input type="text" id="name" size=20 maxlength=20 value=""></td>
                    </tr>
                    <tr style="display:block;" id="enroll1">
                        <td><label>Enrollment No. </label></td>
                        <td><input type="number" id="enroll" style="display:block;" size=20 maxlength=12 value=""></td>
                    </tr>
                    <tr>
                        <td><label>Email </label></td>
                        <td><input type="email" id="emailadd" size=20 maxlength=25 value=""></td>
                    </tr>
                    <tr>
                        <td><label>Mobile No. </label></td>
                        <td><input type="number" id="mobile" size=20 maxlength=10 value=""></td>
                    </tr>
                    <tr>
                        <td><label>Address</label></td>
                        <td><textarea rows="2" cols="20"></textarea></td>
                    </tr>
                    <tr >
                        <td><label>Category</label></td>
                        <td><select id="category" onchange="show()">    <!--onchange show methos is call-->
                                <option value="Student">Student</option>
                                <option value="Parents">Parents</option>
                                <option value="Guardians">Guardians</option>
                            </select>
                        </td>
                    </tr>
                </table><br/>
            <input type="submit" value="Sign Up">
        </form>
    </body>
</html>

其他回答

有趣的后盖(selectObject) 值= selectObject.值; 控制台日志(价值); 的 <选择id=“孔博”“> <option value=" >选择组合</option> <选项价值= >“Value1 Text1 < /选项> <选项价值= Value2 " > Text2 < /选项> <选项价值= >“Value3 Text3 < /选项> 选择< - >

上面的示例为您提供OnChange事件上组合框的选定值。

如何在jQuery中做到这一点:

<select id="yourid">
<option value="Value 1">Text 1</option>
<option value="Value 2">Text 2</option>
</select>

<script src="jquery.js"></script>
<script>
$('#yourid').change(function() {
  alert('The option with value ' + $(this).val() + ' and text ' + $(this).text() + ' was selected.');
});
</script>

你还应该知道Javascript和jQuery是不一样的。jQuery是有效的JavaScript代码,但并不是所有的JavaScript都是jQuery。你应该查找不同之处,并确保你使用的是合适的。

您可以尝试咆哮代码

<select onchange="myfunction($(this).val())" id="myId">
    </select>

如果有人正在寻找一个React解决方案,而不需要下载附加依赖项,你可以这样写:

<select onChange={this.changed(this)}>
   <option value="Apple">Apple</option>
   <option value="Android">Android</option>                
</select>

changed(){
  return e => {
    console.log(e.target.value)
  }
}

确保在构造函数中绑定changed()函数,如下:

this.changed = this.changed.bind(this);

简单:

函数检索(){ 警报(. getelementbyid (SMS_recipient) .options [. getelementbyid(“SMS_recipient”).selectedIndex]。text); }

function retrieve_other() { alert(myForm.SMS_recipient.options[document.getElementById('SMS_recipient').selectedIndex].text); } function retrieve() { alert(document.getElementById('SMS_recipient').options[document.getElementById('SMS_recipient').selectedIndex].text); } <HTML> <BODY> <p>RETRIEVING TEXT IN OPTION OF SELECT </p> <form name="myForm" action=""> <P>Select: <select id="SMS_recipient"> <options value='+15121234567'>Andrew</option> <options value='+15121234568'>Klaus</option> </select> </p> <p> <!-- Note: Despite the script engine complaining about it the code works!--> <input type="button" onclick="retrieve()" value="Try it" /> <input type="button" onclick="retrieve_other()" value="Try Form" /> </p> </form> </HTML> </BODY>

输出: Klaus或Andrew取决于selectedIndex是什么。如果你想要的是值,只需将.text替换为value。然而,如果它只是你想要的值(而不是选项中的文本),那么使用document.getElementById(' sms_receiver ').value