简体   繁体   English

结合两个javascript函数

[英]combine two javascript functions

I've have been trying to fix this problem. 我一直在尝试解决此问题。

Here is the code: 这是代码:

<h4>Your Itinerary</h4>
<div id="selected"></div>
<script>
    (function () {  
        $(".info-css").on(
            'click' 
        ,   function (e) {
                $('#selected').html(
                    [
                        $('#start option:selected').text()                        
                    ,   $('#second option:selected').text()                       
                    ,   $("#datepicker").datepicker( "getDate" )
                    ,   $('#third option:selected').text()
                    ,   $("#datepicker2").datepicker( "getDate" )
                    ,   $('#forth option:selected').text()
                    ,   $("#datepicker3").datepicker( "getDate" )
                    ].join("<br> to ")
                );
            }
        );
    }());
</script>

The code outputs something like this: 代码输出如下:

Your Itinerary

Arenal / la Fortuna

to Cahuita

to Mon Mar 21 2016 00:00:00 GMT-0600 (Central Standard Time (Mexico))

to Bocas del Toro (To/From Costa Rica)

to Thu Mar 24 2016 00:00:00 GMT-0600 (Central Standard Time (Mexico))

to Manzanillo

to Thu Mar 31 2016 00:00:00 GMT-0600 (Central Standard Time (Mexico))

I'm trying to do two things: 我正在尝试做两件事:

1) Have the getDate only show the Mon Mar 21 2016 and not the time and everything else. 1)让getDate仅显示Mon Mar 21 2016 ,而不显示时间和其他所有信息。

2) Combine the 2)结合

    $('#second option:selected').text()
,   $("#datepicker").datepicker( "getDate" )

into one string with on between the two. 为一个字符串on两者之间。

For Example: 例如:

Arenal / la Fortuna

to: 至:

Cahuita on Mon Mar 21 2016   

Here is a test page: 这是一个测试页:

http://thecostaricatoursite.tcct-test.com/test/ http://thecostaricatoursite.tcct-test.com/test/

First of all, you need to set the dateFormat that the datepicker will return by doing the following: 首先,您需要通过执行以下操作来设置datepicker返回的dateFormat

$('#datepicker').datepicker({ dateFormat: 'D M dd yy' });

Notes: You can refer to jquery.datepicker document for more information on how to select the format for date here . 注意:您可以参考jquery.datepicker文档,以获取有关如何在此处选择日期格式的更多信息。

Then get the value of the selected date from the input instead of from getDate to avoid parsing Date object because getDate return a Date instead of a string . 然后从输入而不是从getDate获取所选日期的值,以避免解析Date对象,因为getDate返回Date而不是string

$("#datepicker").val()

Your code to display the itinerary is a comma separated array of string. 您显示行程的代码是用逗号分隔的字符串数组。 Thus to combine the destinate with the date you could do the following 因此,结合目的地和日期,您可以执行以下操作

$('#second option:selected').text() + ' on ' + $("#datepicker").val()

which would treat this as a single element of an array instead of two like your original code. 它将视为数组的单个元素,而不是像原始代码那样将其视为两个元素。 Repeat the same for the rest of your selected date and you will get the result you like. 在您选择的剩余日期中重复相同的操作,您将获得想要的结果。

Good luck!! 祝好运!!

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

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