2014-02-24 85 views
0

我試圖檢索所有複選框的所有值,當我點擊按鈕「extraire」時(請參見下圖)。我想要有一個真值列表,這是複選框的值(如果可能,字段名稱關聯)。動態複選框值播放

控制器:

case class extractionBoxForm(value : Boolean) 
val extractionForm : Form[extractionBoxForm] = Form(
mapping(
    "value" -> boolean 
)(extractionBoxForm.apply)(extractionBoxForm.unapply) 

觀點:

@helper.form(routes.ExtractionController.checkedValues){ 
    @listNameFields.map { fieldName => 
      <div class="form_inputs clearfix clickable"> 
       <div class="row-fluid"> 
        <div class="span3"> 
         <label class="control-label">@fieldName.tail.head :</label> 
        </div> 
        <div class="span1 offset8"> 
         @helper.checkbox(extractionForm("value"),'name ->"rendering", 'class->"chkbox1",'checkboxMap -> "value") 
        </div> 
       </div> 
      </div> 
     } 
     <div class="validForm"> 
      <input type="submit" onclick="sayHello()" value="Extraire" class="btn btn-info"> 
     </div> 
} 


我試過Handling repeated values但我做不到使其與複選框工作

回答

0

最後我得到了它的工作:

播放斯卡拉版本:

<div class="widget_content no-padding"> 
      @helper.form(routes.ExtractionController.checkedValues){ 
      @for(i <- 0 until listNameFields.size) { 
      <div class="form_inputs clearfix"> 
      <div class="row-fluid"> 
       <div class="span5"> 
        <label class="control-label">@listNameFields(i).description :</label> 
       </div> 
       <div id="checkboxlist" class="span1 offset6"> 
       @helper.checkbox(myForm("fields[" + i + "]"), '_label -> "" , 'value -> listNameFields(i).fieldName) 
       </div> 
      </div> 
     </div> 

      } 
      <div class="validForm"> 
       <input type="submit" id="Extract1" value="Extraire" class="btn btn-info"> 
      </div> 
      } 

jQuery的版本:

<script type="text/javascript" src="@routes.ExtractionController.javascriptRoutes">  </script> 

<script> 
$(document).ready(function() { 
/* Get the checkboxes values based on the class attached to each check box */ 
$("#Extract1").click(function() { 
    getValueUsingClass(); 
    }); 
}); 

function getValueUsingClass(){ 
/* declare an checkbox array */ 
var chkArray = []; 

/* look for all checkboes that have a class 'chk' attached to it and check if it was checked */ 
$(".chkbox1:checked").each(function() { 
    chkArray.push($(this).val()); 
}); 

/* we join the array separated by the comma */ 
var selected; 
selected = chkArray.join(',') + ","; 



/* check if there is selected checkboxes, by default the length is 1 as it contains one single comma */ 
if(selected.length > 1){ 
    /* alert("You have selected " + selected);    */ 
}else{ 
    alert("Please at least one of the checkbox"); 
} 

jsRoutes.controllers.ExtractionController.checkedValues(chkArray).ajax({ 
    type: "post", 
    success: function(){ 
    window.location.href = "/extraction" 
    }, 
    error:function(){ 
    } 

}); 
    } 
</script> 
0

每個複選框都需要有不同的值。然後,您可以將這些值綁定到列表。

+0

按價值計算,你的意思是這個領域:「checkboxMap - > 「值」 ?或者我添加一個字段值:'value - >「uniqueValue?」 當我有不同的價值時,我使用「重複值」模型? – GermainGum