如何根据下拉选择自动单击超链接?

时间:2019-04-11 09:34:16

标签: javascript c# jquery .net asp.net-mvc

我希望在下拉列表中选择特定选项时单击特定链接

我尝试过

2019_04_11_074552_add_column_to_users.php

也尝试过

$('#OTST').change(function () {
    if ($('#OTST').val() == "2")
        {
            $("#STSelectedPhase0").trigger('click');
        }
        else if ($('#OTST').val() == "3")
        {
            $("#OTSelectedPhase0").trigger('click');
        }
        else
        {
            $("#AllSelectedPhase0").trigger('click');
        }
});

但是这些都不起作用

这是我的链接代码

 $("#AllSelectedPhase0").click();

,下拉代码为:

@Html.EncodedActionLink("All", "TimeReportingDetails", "TimeReporting", new { companyId = Tlitem.CompanyId, companyWeekEnding = Tlitem.WeekEnding, FileId = Tlitem.FileId, jobId = Tlitem.JobId, jobType = Tlitem.JobType, phaseno = 0, OTST = "" }, new { Id = "AllSelectedPhase0" });

@Html.EncodedActionLink("ST", "TimeReportingDetails", "TimeReporting", new { companyId = Tlitem.CompanyId, companyWeekEnding = Tlitem.WeekEnding, FileId = Tlitem.FileId, jobId = Tlitem.JobId, jobType = Tlitem.JobType, phaseno = 0, OTST = 2 }, new { Id = "STSelectedPhase0" });

@Html.EncodedActionLink("OT", "TimeReportingDetails", "TimeReporting", new { companyId = Tlitem.CompanyId, companyWeekEnding = Tlitem.WeekEnding, FileId = Tlitem.FileId, jobId = Tlitem.JobId, jobType = Tlitem.JobType, phaseno = 0, OTST = 3 }, new { Id = "OTSelectedPhase0" });

1 个答案:

答案 0 :(得分:1)

这里是触发事件单击的问题,但实际上它会返回您的元素。 当您看到$("#STSelectedPhase0").trigger('click')$("#STSelectedPhase0").click()时都返回元素`。

对于自动点击超链接,您应该使用$("#STSelectedPhase0")[0].click()

$("#btngoogle").click(function(){
$("#alink")[0].click()
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<a id="alink" href="https://www.google.com">Test</a>
<input id="btngoogle" type="button" value="Google">
我已将代码放在按钮点击触发器href上。

让我知道是否需要更多信息。 :)