调用复选框和按钮函数



在javascript中可以通过一次点击调用两个函数吗?单独的函数实现编码如下:

 <input type="checkbox" id="3" class="radio" value="3" onchange="checkbox(this.id)" name="c1"></input>

,第二个是

<button type="button" onclick="calculation()">Submit</button>

我想在一次点击中调用这两个函数。这可能吗?

第一个函数编码是这样的

function checkbox(clicked_id)
{   
    var checkbox = document.getElementById(clicked_id);
    if(checkbox.checked==true)
    {
         // coordintaes for routes 

        //var m=65;
        // Get coordinates of route and display it
        for(m=1;m<=36;m++){
            //alert(m);
        $.ajax({
        url: "route_query4district.php?id="+m,
        dataType: 'json',
        success: function (msg1) 
        { 
        xarray1 = new Array();
         yarray1 = new Array();
         color1 = new Array();
        js_data1 = eval(msg1);
        //  alert(js_data1);
            var k1=0;
            for(j1=0;j1<js_data1.length;j1+=3){
            xarray1[k1] = js_data1[j1];
            yarray1[k1] = js_data1[j1+1];
            color1[m] = js_data1[j1+2];
            k1++;
            }
            alert(color1[1]);
            var flightPlanCoordinates = new Array();
            for(i=0;i<xarray1.length;i++){
             flightPlanCoordinates[i] = new google.maps.LatLng(yarray1[i], xarray1[i]);
            }
            var contentString = '<div id="content">';
            var infowindow = new google.maps.InfoWindow({
              content: contentString
          });

            var mid_color=67+((parseInt(checkbox.value)));
            var last_color=36+((parseInt(checkbox.value)));
            var line_color=color1[m];
            var stroke_color=line_color.toString();
            var linee_color=color1;
            var strokee_color=linee_color.toString();
            flightPath[m] = new google.maps.Polygon({
                path: flightPlanCoordinates,
                strokeColor: stroke_color,
                fillColor: line_color,
                strokeOpacity: 1.0,
                strokeWeight: 4
          });
          flightPath[m].setMap(map);
        google.maps.event.addListener(flightPath, 'click', function() {
        infowindow.open(map,flightPath);
      });
         }
        });
        }
    }   // if checkbox is checked

,第二个函数编码为

function calculation(){
           var laastyear= document.getElementById("LasttYear").value;
           var firstyeaar= document.getElementById("FfYear").value;
           var projecteedyear=  document.getElementById("ProjectedYear").value;
            // alert("hh");
            $.ajax({
            url: "test_dictrict_projections_folrmula.php?LastYear="+laastyear+"&FYEAR="+firstyeaar+"&ProjectYear="+projecteedyear,
          });
};
};

是的,onclick只是一个javascript部分。你可以把整个函数放在这里。像这样的代码应该可以工作:

onclick="calculation();checkbox(this.id);"

我不想听起来像个傻瓜。但是你试过在标签中添加两个事件监听器吗??

类似:

<!DOCTYPE html>
<html lang = 'es'>
    <head>
        <title> MY TEST </title>
    </head>
    <body>
        <input type = 'button' value = 'Lets DO IT!' id = 'button'>
        <script>
            var inputButton = document.getElementById('button');
            inputButton.addEventListener('click', show1);
            inputButton.addEventListener('click', show2);
            function show1(){
                alert('Message 1');
            }
            function show2(){
                alert('Message 2');
            }
        </script>
    </body>
</html>

相关内容

  • 没有找到相关文章

最新更新