简体   繁体   English

在模式框上的JQuery Checked Selector并将其返回到页面上

[英]JQuery Checked Selector on a modal-box and return it on the page

What I want to do is, return the value checked on each checkbox on a modal-box to a text-box. 我想要做的是,将模态框上每个复选框上选中的值返回到文本框。 This question is quite similar to this one , but the aim of the question is different. 这个问题是非常相似这一个 ,但问题的目的是不同的。 So please take a look both of them. 所以请看看他们两个。 :) :)

I've the code to initialize the modal-box: 我有初始化模式框的代码:

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

As you see above, I am trying to use pseudo-selector function: input[name='checkedID[]']:checked but failed to return the value. 如上所示,我正在尝试使用伪选择器函数: input[name='checkedID[]']:checked但无法返回值。 Or there is something I missed for the code? 或者我错过了代码?

And here is the textbox for return the value checked on the modal-box: 这里是用于返回模态框中选中的值的文本框:

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

The code able is to show the modal-box. 代码能够显示模态框。 But how to return the "Product" chosen from each textbox by the user to the textbox products_id_textbox ? 但是如何将用户从每个文本框中选择的“产品”返回到文本框products_id_textbox Thanks. 谢谢。

The problem solved with this code: 这段代码解决了这个问题:

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

and a modification on the button that just for closing the modal-box: 以及仅用于关闭模态框的按钮的修改:

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

That function called :checked Selector . 该函数调用:选中选择器

Thank you very much and hope helps... :) 非常感谢,希望有帮助... :)

What you are outputting from php is checkedID not checkedID[] Change 你从php输出的是checkID not checkedID []更改

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

to

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

Please try below code: 请尝试以下代码:

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

Hope this will help you... 希望对你有帮助...

Your selector should look like, 你的选择器看起来像,

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

It gives you all the checkbox with name "checkedID[]" is checked. 它会为您提供所有名称为“checkedID []”的复选框。 you have to loop it to get all the values. 你必须循环它以获得所有的价值。

Hope this helps ! 希望这可以帮助 !

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

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