如何使用jquery从选择框中获取和设置所选选项 [英] how to get and set selected option from select box using jquery
问题描述
我有一个选择框的html,当我从这个选择框中选择选项时,我在其上添加了一些选项。整个页面正在刷新。我想使用jquery显示选择框的选定选项如何在选择框中选择文本。以下是按代码
i have a select box of html and i added some option on it, when i select option from this select box. the whole page is being refresh. i want to display selected option of select box using jquery how can i do selected text in select box. below is by code
<select id="SelectItem" onchange="Settings();">
<option value="-1">Select</option>
<option value="0">Supervisors</option>
<option value="1">Solid Surface Company</option>
<option value="2">Comment Reason</option>
</select>
< script type =text / javascript>
函数设置(){
var x = $(#SelectItem)。val();
var y = $(# SelectItem选项:选中)。text();
var url =;
if(x == - 1){
url =Home / Settings;
}
if(x ==0){
url =主管/索引;
}
if(x ==1){
url =SSCompany / Index;
}
if(x ==2){
url =C ommentsReason / Index;
}
var OriginalUrl = window.location.protocol +'//'+ window.location.hostname +(window.location.port? (':'+ window.location.port):'')+/+ url;
window.location.replace(OriginalUrl);
}; < br $> b $ b
< / script>
<script type="text/javascript">
function Settings() {
var x = $("#SelectItem").val();
var y = $("#SelectItem option:selected").text();
var url = "";
if (x == "-1") {
url = "Home/Settings";
}
if (x == "0") {
url = "Supervisors/Index";
}
if (x == "1") {
url = "SSCompany/Index";
}
if (x == "2") {
url = "CommentsReason/Index";
}
var OriginalUrl = window.location.protocol + '//' + window.location.hostname + (window.location.port ? (':' + window.location.port) : '') + "/" + url;
window.location.replace(OriginalUrl);
};
</script>
推荐答案
(#SelectItem)。val();
var y =
("#SelectItem").val();
var y =
(#SelectItem option:selected)。text();
var url =;
if(x == - 1){
url =Home / Settings;
}
if(x ==0){
url =Supervisors / Index;
}
if(x ==1 ){
url =SSCompany / Index;
}
if(x ==2){
url =CommentsReason / Index;
}
var OriginalUrl = window.location.protocol +'//'+ window.location.hostname +( window.location.po rt?(':'+ window.location.port):'')+/+ url;
window.location.replace(OriginalUrl);
} ;
< / script>
("#SelectItem option:selected").text();
var url = "";
if (x == "-1") {
url = "Home/Settings";
}
if (x == "0") {
url = "Supervisors/Index";
}
if (x == "1") {
url = "SSCompany/Index";
}
if (x == "2") {
url = "CommentsReason/Index";
}
var OriginalUrl = window.location.protocol + '//' + window.location.hostname + (window.location.port ? (':' + window.location.port) : '') + "/" + url;
window.location.replace(OriginalUrl);
};
</script>
这篇关于如何使用jquery从选择框中获取和设置所选选项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!