简体   繁体   English

如何从选定的下拉列表中创建变量并使用javascript返回

[英]How to create a variable from a selected dropdown list and return it using javascript

I have an HTML page with a dropdown list, which represent specific time as follow: 我有一个带有下拉列表的HTML页面,该列表表示特定时间,如下所示:

  <div class="dropdown form-group col-xs-5" style="padding-right: 20px;">
        <label for="time">Temps</label>
        <button class="btn btn-default dropdown-toggle" type="button" id="dropdownMenu1" data-toggle="dropdown" aria-haspopup="true" aria-expanded="true">
   5 sec<span class="caret"></span>
        </button>
        <ul class="dropdown-menu" aria-labelledby="dropdownMenu1" id="timeChoice">
            <li><a href="#" id="act1">5 sec</a></li>
            <li><a href="#" id="act2">10 sec</a></li>
            <li><a href="#" id="act3"> 15 sec</a></li>
            <li><a href="#" id="act4">30 sec</a></li>
        </ul>
 </div> 

I want to create a function which identify the time and return a value assigned with this time. 我想创建一个识别时间并返回与此时间分配的值的函数。 For instance when user click sec5 the function put in the variable timeOption = 5000; 例如,当用户单击sec5该函数将变量timeOption = 5000; I use javascript and jQuery for that: 我为此使用javascript和jQuery:

$('#act1').click(function(e) {
    var timeOption = 5000;
    alert(timeOption);
    e.preventDefault();// prevent the default anchor functionality
});
$('#act2').click(function(e) {
    var timeOption = 10000;
    alert(timeOption);
    e.preventDefault();// prevent the default anchor functionality
});
$('#act3').click(function(e) {
    var timeOption = 15000;
    alert(timeOption);
    e.preventDefault();// prevent the default anchor functionality
});
$('#act4').click(function(e) {
    var timeOption = 30000;
    alert(timeOption);
    e.preventDefault();// prevent the default anchor functionality
});

How to add a return statement with specific value ? 如何添加具有特定值的return语句?

You are asking for a return statement, but that is not the right approach, because an event callback isn't called from a context where you can handle the function's return value (the return value ends up in a jQuery function and gets handled there). 您正在要求一个return语句,但这不是正确的方法,因为没有从可以处理函数返回值的上下文中调用事件回调(返回值最终在jQuery函数中并在那里得到处理) 。 You want to make your timeOption value visible to some other function, which you can do in various ways. 您想使timeOption值对其他函数可见,您可以通过各种方式进行操作。 In this case, I would suggest passing the value to a function: 在这种情况下,我建议将值传递给函数:

$('#act1').click(function(e) {
    var timeOption = 5000;
    alert(timeOption);
    e.preventDefault();// prevent the default anchor functionality

    handleTimeOption(timeOption);
});

$('#act2').click(function(e) {
    var timeOption = 10000;
    alert(timeOption);
    e.preventDefault();// prevent the default anchor functionality

    handleTimeOption(timeOption);
});    
$('#act3').click(function(e) {
    var timeOption = 15000;
    alert(timeOption);
    e.preventDefault();// prevent the default anchor functionality

    handleTimeOption(timeOption);
});
$('#act4').click(function(e) {
    var timeOption = 30000;
    alert(timeOption);
    e.preventDefault();// prevent the default anchor functionality

    handleTimeOption(timeOption);
});

function handleTimeOption(timeOption) {
    // Here goes whatever code you want to run when a time option has been selectedl
}

Change your code little bit. 稍微更改您的代码。 Add data attribute to every anchor tag and then write click handler 将数据属性添加到每个定位标记,然后编写点击处理程序

 $('#timeChoice').on("click", "a", function(e) { var timeOption = e.target.getAttribute('data-time')*1000; alert(timeOption); e.preventDefault();// prevent the default anchor functionality }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="dropdown form-group col-xs-5" style="padding-right: 20px;"> <label for="time">Temps</label> <button class="btn btn-default dropdown-toggle" type="button" id="dropdownMenu1" data-toggle="dropdown" aria-haspopup="true" aria-expanded="true"> 5 sec<span class="caret"></span> </button> <ul class="dropdown-menu" aria-labelledby="dropdownMenu1" id="timeChoice"> <li><a href="#" id="act1" data-time="5">5 sec</a></li> <li><a href="#" id="act2" data-time="10">10 sec</a></li> <li><a href="#" id="act3" data-time="15"> 15 sec</a></li> <li><a href="#" id="act4" data-time="30">30 sec</a></li> </ul> </div> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM