簡體   English   中英

帶有JavaScript的Xforms-確定復選框處於選中狀態還是未選中狀態

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

我正在開發一個使用BetterFORMS的表單,該表單允許用戶檢查哪些字段適用於它們,然后發送數據進行處理。 表單是完整且可以正常工作的-我只想添加一些表單驗證,如果未選中任何復選框,則該表單驗證將阻止用戶發送請求。

模型和命名空間:

<?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> 

我的表格:

<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>

想法是,顯示給用戶的“提交”按鈕調用javascript函數checkForm(),然后被禁用。

javascript函數查看我的每個文本框,如果至少選中了一個文本框,則使用maskButton提交來發送表單,該表單對用戶是隱藏的。 否則,顯示警報消息。

我一直在使用getElementById()函數,但是似乎無法從我的復選框(選中或未選中)中獲取任何值,所以這就是為什么我只嘗試在警報中顯示該值。 此警報應顯示Y為已選中,如果未更改或未選中則為空(與表單提交相同)。 但是,無論我嘗試了什么,警報都是空的。

另一種方法是在表單后添加確認頁面,因此不會丟失所有內容。 我要你看看我想做的事是否可行。 謝謝

沒有任何Javascript指令,您可以使用xforms:group技巧來呈現特定條件的Submit控件。 像這樣:

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

-Alain

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM