简体   繁体   English

带有JavaScript的Xforms-确定复选框处于选中状态还是未选中状态

[英]Xforms with javascript - determine if checkbox is checked or unchecked

I am developing a form using betterFORMS which allows a user to check which fields apply to them and then send the data for processing. 我正在开发一个使用BetterFORMS的表单,该表单允许用户检查哪些字段适用于它们,然后发送数据进行处理。 The form is complete and working - I just wanted to add some form validation which stops a user sending a request if none of the checkboxes are checked. 表单是完整且可以正常工作的-我只想添加一些表单验证,如果未选中任何复选框,则该表单验证将阻止用户发送请求。

Model and Namespaces: 模型和命名空间:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0"
xmlns="http://www.w3.org/2002/06/xhtml2" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xforms="http://www.w3.org/2002/xforms" xmlns:xsd="http://www.w3.org/2001/XMLSchema"
<xsl:output method="xml" />
<xsl:template match="/">
<xforms:model id="documentsChecklist">
    <xforms:instance>   
        <actionform xmlns="">
            <xforms:model id="documentsChecklist">
                <xforms:instance>   
                <actionform xmlns="">
                    <detail>        
                        <other></other>
                        <otherText></otherText>
                    </detail>
                </actionform>
    </xforms:instance>
    <xforms:bind id="other" nodeset="/actionform/detail/other" calculate="choose(. = 'Y', ., 'N')"/>
    <xforms:bind nodeset="/actionform/detail/otherBox" relevant="/actionform/detail/other ='Y'" /> 
</xforms:model> 

My form: 我的表格:

<div id="formBody"><br />
    <xforms:select bind="other" appearance="full" align="right">
        <xforms:item>
            <xforms:label>Other</xforms:label>
            <xforms:value>Y</xforms:value>
        </xforms:item>
    </xforms:select>
    <xforms:input ref="/actionform/detail/otherText">
        <xforms:label>Please specify:*</xforms:label>
    </xforms:input>
    <xforms:submit submission="formSubmit" id="send" class="ui-button" onclick="checkForm(this);return false;">
        <xforms:label>Send</xforms:label>
    </xforms:submit>
</div>

    <xforms:submit submission="formSubmit" id="maskButton" class="ui-button">
        <xsl:attribute name="style">display:none</xsl:attribute>
    </xforms:submit>

    <script type="text/javascript">
        function checkForm(){
            var otherCheckValue = document.getElementById('otherCheck-value');
            alert(otherCheckValue.value);
            if(boxesChecked != 0){
                document.getElementById('maskButton-value').click();
            }
        }
    </script>

</xsl:template>
</xsl:stylesheet>

The idea is that the submit button displayed to the user calls the javascript function checkForm() and is then disabled. 想法是,显示给用户的“提交”按钮调用javascript函数checkForm(),然后被禁用。

The javascript function looks at each of my text boxes and if at least one is checked, sends the form using the maskButton submit, which is hidden from the user. javascript函数查看我的每个文本框,如果至少选中了一个文本框,则使用maskButton提交来发送表单,该表单对用户是隐藏的。 Otherwise, display an alert message. 否则,显示警报消息。

I have been playing around with the getElementById() function but cannot seem to get any value out of my checkbox (checked or unchecked), so that is why I am only trying to display the value in an alert. 我一直在使用getElementById()函数,但是似乎无法从我的复选框(选中或未选中)中获取任何值,所以这就是为什么我只尝试在警报中显示该值。 This alert should show a Y for checked or empty if unchanged or unchecked (same as the form submission). 此警报应显示Y为已选中,如果未更改或未选中则为空(与表单提交相同)。 But no matter what I have tried, the alert is empty. 但是,无论我尝试了什么,警报都是空的。

Another way would be to add a confirm page after the form, so all is not lost. 另一种方法是在表单后添加确认页面,因此不会丢失所有内容。 I am asking you to see if what I want to do is possible. 我要你看看我想做的事是否可行。 Thanks 谢谢

Without any Javascript instruction, you could use the xforms:group trick to render the submit control for a specific condition. 没有任何Javascript指令,您可以使用xforms:group技巧来呈现特定条件的Submit控件。 Something like this: 像这样:

<xforms:group ref=".[detail/other != '']">
  <xforms:submit .../>
</xforms:group>

-Alain -Alain

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

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