简体   繁体   English

如何点击HTML变量更改?

[英]How can I onclick an HTML variable change?

I am working in HTML and Coldfusion. 我正在使用HTML和Coldfusion。 I want to set a variable equal to zero in CF, then have an onclick event trigger a script that changes the value in the HTML variable to a one. 我想在CF中设置一个等于0的变量,然后让onclick事件触发一个脚本,该脚本将HTML变量中的值更改为1。 Then I can run a cfif that is true if there is a one in the variable. 然后,如果变量中存在一个cfif,则可以运行它。 Does anyone know the best way to do this? 有谁知道最好的方法吗? This is what I am trying, but the onClick does not trigger anything useful. 这是我正在尝试的方法,但是onClick不会触发任何有用的操作。

    <cfset dayHolder = 0>

<!--- Make a Submit Button  --->
<div style="max-width:50%; margin: auto; padding-top: 20px; text-align: center">
    <input type="button" name="submit" onclick="setupVar();" value="Submit Day Page" tooltip="Submits Your Hour by Hour"
     style="font-family:Times New Roman; font-size:16px;"/>
</div>
    <script>

    function setupVar(){
        dayHolder=1;
    return dayHolder;
    }
    </script>

    <cfoutput> #dayHolder#</cfoutput>
    <br>

     <!--- Send the data to the database --->
    <cfif 'dayHolder' eq 1> 
    etc...

Thank you very much in advance. 提前非常感谢您。

You are mixing your ideas a little bit, Javascript runs on the clients browser while CF runs on the server, so to make the changes you are trying to make you'll need to submit a form. 您有点混在一起,Javascript在客户端浏览器上运行,而CF在服务器上运行,因此要进行更改,您需要提交表单。 Here is an example that should help you: 这是一个可以帮助您的示例:

<cfif structKeyExists(FORM,"dayHolder")>
    <cfset dayHolder = FORM.dayHolder>
<cfelse>
    <cfset dayHolder = 0>
</cfif>

<!--- Make a Submit Button  --->
<form>
    <input type="hidden" name="dayHolder" id="dayHolder" value="0">
    <div style="max-width:50%; margin: auto; padding-top: 20px; text-align: center">
        <input type="submit" name="submit" onclick="setupVar();" value="Submit Day Page" tooltip="Submits Your Hour by Hour"
         style="font-family:Times New Roman; font-size:16px;"/>
    </div>
</form>
<script>

function setupVar(){
    document.getElementById('dayHolder').value = '1';
    return true;
}
</script>

<cfoutput> #dayHolder#</cfoutput>
<br>

 <!--- Send the data to the database --->
<cfif dayHolder eq 1> 

*updated according to comment. *根据评论进行了更新。

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

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