简体   繁体   English

切换jQuery切换按钮时运行服务器端脚本

[英]Run server-side script when jQuery Switch button is switched

I have the following jquery plugin on my client side which gives me a on/off switch. 我的客户端上有以下jquery插件 ,它为我提供了开/关开关。 I need "off" and "on" to trigger different server side scripts. 我需要“关闭”和“打开”来触发不同的服务器端脚本。 I know how this can be achieved with a regular "on-click" button, but not with a switch button. 我知道如何通过常规的“单击”按钮而不是开关按钮来实现。

在此处输入图片说明

Here is what's in client-side: 这是客户端中的内容:

<div id="second_div">
            <form>

            <div style="float: left; width: 50%;">
                <p style="padding-bottom: 13px;"><em>Checkbox</em></p>

                <p><input type="checkbox" name="check-1" value="4" class="lcs_check" autocomplete="off" /></p>

            </div>


            </form>
            <div style=" clear: both;"></div>
        </div>

        <div id="third_div">
            <em style="color: #777; font-size: 14px;">Check your browser console to see triggered events</em>
        </div>    
    <p></p>

I would like "off" to trigger one function "google.script.run.loggOff(); 我希望“关闭”以触发一个函数“ google.script.run.loggOff();

I would like "on" to trigger another function "google.script.run.loggOn(); 我想“打开”以触发另一个函数“ google.script.run.loggOn();

This is usually how i do it with an 'onclick button': 这通常是我使用“ onclick按钮”的方式:

Client Side (on-click button): 客户端(单击按钮):

<button type="button" onclick="google.script.run.createDoc();">Create Google Doc</button>


<script>
  google.script.run.createDoc();
</script>

Server Side (on-click button): 服务器端(单击按钮):

function createDoc() {
   var copyDoc = DocumentApp.openById(copyId);
      copyDoc.saveAndClose(); 
}

Maybe I'm misunderstanding the question, but if you want to add click functions the on and off you can do something like this. 也许我误解了这个问题,但是如果您要添加点击功能,则可以打开和关闭,您可以执行以下操作。

//off
$('.lcs_check').click(function() {
   $('.lcs_check').attr('id','loggOff');    
   google.script.run.loggOff();
});

//on
$('.lcs_check #loggOff').click(function() {
    $('.lcs_check #loggOff').removeAttr('id','loggOff');
    google.script.run.loggOn();
});

This plugin is LC-switch, i found the github page and a more detailled demo page . 这个插件是LC-switch,我找到了github页面和一个更详细的演示页面

In the demo page there is a Event section, there is 3 events for what you trying to accomplish. 在演示页面中,有一个“事件”部分,其中包含3个您要完成的事件。

// triggered each time a field changes status
$('body').delegate('.lcs_check', 'lcs-statuschange', function() {
    var status = ($(this).is(':checked')) ? 'checked' : 'unchecked';
    console.log('field changed status: '+ status );
});

// triggered each time a field is checked
$('body').delegate('.lcs_check', 'lcs-on', function() {
     console.log('field is checked');
});

// triggered each time a field is unchecked
$('body').delegate('.lcs_check', 'lcs-off', function() {
     console.log('field is unchecked');
});

But it look like each of this event are called after state has changed. 但是看起来每个事件在状态更改后都会被调用。 I didn't find a way to have click information before state is changed. 在状态更改之前,我没有找到获取点击信息的方法。

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

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