我是JavaScript和jQuery的新手。我想显示一个combobox-A,这是一个HTML <选择>与其选择的id和内容在onChange()的其他地方。
如何通过其选择id完整的组合框,以及如何通过onChange事件的其他参数?
我是JavaScript和jQuery的新手。我想显示一个combobox-A,这是一个HTML <选择>与其选择的id和内容在onChange()的其他地方。
如何通过其选择id完整的组合框,以及如何通过onChange事件的其他参数?
当前回答
简单:
函数检索(){ 警报(. 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
其他回答
简单:
函数检索(){ 警报(. 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
您可以尝试咆哮代码
<select onchange="myfunction($(this).val())" id="myId">
</select>
有趣的后盖(selectObject) 值= selectObject.值; 控制台日志(价值); 的 <选择id=“孔博”“> <option value=" >选择组合</option> <选项价值= >“Value1 Text1 < /选项> <选项价值= Value2 " > Text2 < /选项> <选项价值= >“Value3 Text3 < /选项> 选择< - >
上面的示例为您提供OnChange事件上组合框的选定值。
这对我有帮助。
选择:
$('select_tags').on('change', function() {
alert( $(this).find(":selected").val() );
});
For radio / checkbox:
$('radio_tags').on('change', function() {
alert( $(this).find(":checked").val() );
});
函数setMyValue(v) { console.log (v); } <选择onchange = " setMyValue (this.value)" > <选项值= " " > 1 > < /选项 <选项值= " b " > 2 > < /选项 <选项值= " c " > 3 < /选项> < /选择>