2012-10-17 54 views
4

我想要做的是將模式框上每個複選框上選中的值返回到文本框。這個問題與this one非常相似,但問題的目的不同。所以請看看他們兩個。 :)JQuery格式化選擇器並將其返回頁面

我有代碼初始化模式盒:

<script> 
var grid_modal_options = { 
    height: 'auto', 
    width: '80%', 
    modal: true 
}; 
function showProductsModalBox() { 
    $("#products_modal_box").dialog(grid_modal_options); 
    $("#products_modal_box").parent().appendTo('form:first'); 
} 
</script> 

<div id="products_modal_box" title="Products" style="display: none;"> 
    <div class="in"> 
    <div class="grid-12-12"> 
     <form id="products_modal_box_form" action="#" method="post"> 
     <a href=\"javascript:;\" onclick=\"$('#products_id_textbox').val(\"input[name='checkedID[]']:checked\");$('#products_modal_box').dialog('close')();\">Submit</a> 
     <table> 
      <thead> 
      <tr> 
       <th>&nbsp;</th> 
       <th>Product</th> 
      </tr> 
      </thead> 
      <!-- Query for read mysql goes here (I skipped this line because it's not the main thing I'm gonna ask since it's run well) /--> 
      <tbody> 
      <?php 
      //read the results 
      while($fetch = mysqli_fetch_array($r)) {     
       print "<tr>"; 
       print " <td><input type='checkbox' name='checkedID' value='" . $fetch[0] . "' /></td>"; //--> How to get the value of $fetch[0], collect it and return to a textbox? 
       print " <td>" . $fetch[0] . "</td>"; //$fetch[0] == Product ID 
       print "</tr>"; 
      } 
      ?> 
      </tbody> 
     </table> 
     </form> 
    </div> 
    </div> 
</div> 

正如你看到的上面,我試圖用pseudo-selector功能:input[name='checkedID[]']:checked但未能返回值。或者我錯過了代碼?

這裏是迴歸文本框的模式盒檢查值:

<input type='text' id='products_id_textbox' name='products_id_textbox' /> 
<a href='#' onclick='showProductsModalBox(); return false;'>Choose products</a> 

幹練的代碼是顯示模式盒。但是,如何將用戶從每個文本框中選擇的「產品」返回到文本框products_id_textbox?謝謝。

回答

3

這段代碼解決的問題:

<script> 
$("input#checkedID").change(function() { 
    var str = ""; 
    $("input:[name='checkedID[]']:checked").each(function() { 
     str += $(this).val() + ", "; 
    }); 
    $("input#products_id_textbox").val(str); 
}) 
.trigger('change'); 
</script> 

和按鈕,只是關閉模態框修改:

<a href=\"javascript:;\" onclick=\"$('#products_modal_box').dialog('close')();\">Submit</a> 

該函數調用:checked Selector

非常感謝你,希望可以幫助... :)

0

什麼你從PHP的輸出是checkedID不checkedID [] 更改

print " <td><input type='checkbox' name='checkedID' value='" . $fetch[0] . "' /></td>"; 

print " <td><input type='checkbox' name='checkedID[]' value='" . $fetch[0] . "' /></td>"; 
+0

IC啊,謝謝,但它仍然無法正常工作...... –

+0

似乎有上的onclick錯誤,請嘗試wrinting爲了清楚而以功能。你可能必須在$('input [name =「checkedID []」]:checked')上使用$ .each來獲取值 – xelber

0

請嘗試以下代碼:

myCheckbox is a class which you need to assign your checkboxes.. 

    var checkboxValues = $('.myCheckbox:checked').map(function() { 
     return $(this).val(); 
    }).get(); 


Then you can assign that values into your textbox.... 

$('#products_id_textbox').val(checkboxValues); 

希望這將幫助你...

0

你的選擇應該是什麼樣子,

$("input[name='checkedID[]']:checked") 

它可以讓你所有的名字複選框「checkedID []」被選中。你必須循環它才能獲得所有的值。

希望這會有所幫助!