Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/403.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/fsharp/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用Javascript获取所选选项ID_Javascript_Html - Fatal编程技术网

使用Javascript获取所选选项ID

使用Javascript获取所选选项ID,javascript,html,Javascript,Html,我在每个选项卡中都有一个带有选择选项的选项卡,我希望当我点击提交按钮时,该选项卡在分配给它们的特定URL中提交。我用javascipt做这个。我是javascript的新手,所以我需要你的慷慨帮助。 我使用选项卡根据其类别划分了选择选项,因此我的代码如下所示: HTML <ul class="ut-nav-tabs clearfix"> <li class="active"><a href="#t1Tab" data-toggle="tab">Corp

我在每个选项卡中都有一个带有选择选项的选项卡,我希望当我点击提交按钮时,该选项卡在分配给它们的特定URL中提交。我用javascipt做这个。我是javascript的新手,所以我需要你的慷慨帮助。 我使用选项卡根据其类别划分了选择选项,因此我的代码如下所示: HTML

<ul class="ut-nav-tabs clearfix">
    <li class="active"><a href="#t1Tab" data-toggle="tab">Corporate Services</a>
    </li>
    <li class="">
    <a href="#t2Tab" data-toggle="tab">Digital Marketing</a>
    </li>
</ul>

<div id="t1Tab" class="tab-pane clearfix active">
    <form method="post" action="/trendstatic15/#wpcf7-f3409-o1">
        <select id="id_corporateservices">
            <option value="Can a foreign investors open his own company in the Philippines?">Can a foreign investors open his own company in the Philippines?</option>
            <option value="What are the business regulations currently used in the Philippines?">What are the business regulations currently used in the Philippines?</option>
        </select>
        </div>
        <input class="wpcf7-form-control wpcf7-submit" type="submit" value="Submit">
    </form>
</div>

    <div id="t2Tab" class="">
        <form method="post" action="/trendstatic15/#wpcf7-f3409-o1">
            <select id="id_digitalservices">
            <option value="Should I add online video to my web sites?">Should I add online video to my web sites?</option>
            <option value="What works best in Internet marketing?">What works best in Internet marketing?">What works best in Internet marketing?">What works best in Internet marketing?</option>
            </select>
            </div>
            <input class="wpcf7-form-control wpcf7-submit" type="submit" value="Submit">
        </form>
    </div>
外国投资者能否在菲律宾开设自己的公司? 菲律宾目前使用的商业法规是什么? 我应该将在线视频添加到我的网站吗? 什么在网络营销中效果最好?”>什么在网络营销中效果最好?”>什么在网络营销中效果最好?
JAVASCRIPT

<script type="text/javascript">    
    function redirect() {
        var filename_corporate = document.getElementById('id_corporateservices').value;
        var filename_digital = document.getElementById('id_digitalservices').value;

        var url ='';

        if (filename_digital == 'What works best in Internet marketing?')
        {
            url= 'http://localhost/testsite/digital-page';
        }
        else if(filename_corporate == 'Can a foreign investors open his own company in the Philippines?')
        {
            url= 'http://localhost/testsite/corporate-page';
        }
        else
        {
         url= 'http://anyurlpage.com';
        }

        window.location = url;

        }
</script>

函数重定向(){
var filename\u corporate=document.getElementById('id\u corporateservices')。值;
var filename\u digital=document.getElementById('id\u digitalservices')。值;
var url='';
如果(filename_digital==“什么在互联网营销中最有效?”)
{
url='1〕http://localhost/testsite/digital-page';
}
否则,如果(filename_corporate==“外国投资者可以在菲律宾开设自己的公司吗?”)
{
url='1〕http://localhost/testsite/corporate-page';
}
其他的
{
url='1〕http://anyurlpage.com';
}
window.location=url;
}

为什么我在选项中选择了任意,然后将其始终直接提交到第一个url条件?

因为您总是引用id为“id\u digitalservices”且永不更改的元素?无论您是否更改选项卡可见性,它仍然保留在DOM中

您可以使用以下代码获取实际更改的选项值:

<script type="text/javascript">  
function selectmenuOnchange(selectMenuId) {

    //Get the selectmenu element by Id
     var selectmenuID = document.getElementById(selectMenuId);
     //Get selected options value
     var optionValue = selectmenuID.options[selectmenuID.selectedIndex].value;

     //this should return the selectmenu's id
     alert(this.id);

     redirect(optionValue);

}

function redirect(optionValue) {

    var url ='';

    if (optionValue === 'What works best in Internet marketing?')
    {
        url= 'http://localhost/testsite/digital-page';
    }
    else if(optionValue === 'Can a foreign investors open his own company in the Philippines?')
    {
        url= 'http://localhost/testsite/corporate-page';
    }
    else
    {
     url= 'http://anyurlpage.com';
    }

    window.location = url;
}
</script>

函数selectmenuOnchange(selectMenuId){
//按Id获取selectmenu元素
var selectmenuID=document.getElementById(selectmenuID);
//获取所选选项值
var optionValue=selectmenuID.options[selectmenuID.selectedIndex].value;
//这将返回selectmenu的id
警报(this.id);
重定向(optionValue);
}
函数重定向(optionValue){
var url='';
如果(optionValue==“什么在互联网营销中最有效?”)
{
url='1〕http://localhost/testsite/digital-page';
}
否则,如果(optionValue==‘外国投资者能否在菲律宾开设自己的公司?’
{
url='1〕http://localhost/testsite/corporate-page';
}
其他的
{
url='1〕http://anyurlpage.com';
}
window.location=url;
}
您需要编辑两个SelectMenu,以包含my onchange功能,如下所示:

HTML



localhost
是计算机引用计算机的方式;你的HTML格式不正确,这意味着开始标记没有匹配的结束标记。请调整你的问题以便于阅读Vihan1086,我只是给出了一个示例url,因为现在我正在localhostochi中开发它,是的,再次对html进行了更改,你能再次查看吗?提前感谢Hi Defain,我真的不知道DOM,我可以在我的代码上更改什么?如果我正确理解您的意图,我的答案中的代码应该可以工作。但是你不应该像在那里那样比较字符串。期权价值应该是其他的东西;例如整数。您添加的代码仍然不起作用,它仍然指向第一个条件URL。当然,您需要对另一个选择菜单执行相同的操作。当id\u digitalservices selectmenu具有给定值时,您的代码始终接受第一个if子句,而不管其他人有什么值。如果它有另一个值,并且id_corporateservices菜单有正确的值,则会选择它。我将其修改为:
var selectmenuID2=document.getElementById('id_corporateservices')。value;var filename\u corporate=selectmenuID2.options[selectmenuID2.selectedIndex].value;var selectmenuID=document.getElementById(“id_digitalservices”);var filename\u digital=selectmenuID.options[selectmenuID.selectedIndex].value
<select onchange="selectmenuOnchange(this.id);">