【问题标题】:Passing Value Chosen from Modalbox to each Textbox in PHP将从模态框中选择的值传递给 PHP 中的每个文本框
【发布时间】:2012-08-24 01:37:19
【问题描述】:

this thread >>

如何将模式框中选择的值传递给基于其行的每个文本框?您可以动态添加或删除文本框,为了填充文本框,您必须从模式框中选择值.

例如:

[Add Textbox] [Remove Textbox]
[TextBox1] [Link]
[TextBox2] [Link]
 ...
[TextBoxX] [Link]

当点击链接时,它会显示一个模态框,供用户选择模态框上提供的表格上的列表并将值返回到其行上的文本框。我创建了下面的代码,但它返回每个文本框的值。

请看下面的代码:

此 javascript 代码用于添加/删除文本框:

<script type="text/javascript">
       //Add
       $('#tblDetail tbody').on("click", "a", function() {
         $("#products_modal_box").data("target", $(this).siblings("input"));
         showProductsModalBox();
         return false;
       });

       $(document).ready(function() {
         $("#addRow").click(function() {
           $('#tblDetail tbody>tr:last').clone(true).insertAfter('#tblDetail tbody>tr:last');
           $('#tblDetail tbody>tr:last #field1').val(''); 
           $('#txtTotalRow').val(tblDetail.rows.length - 1);
         });
       });  

       function removeRowFromTable() {
         var tbl = document.getElementById('tblDetail');
         var lastRow = tbl.rows.length;
         if (lastRow > 2) tbl.deleteRow(lastRow - 1);
       }
</script>

这是添加/删除文本框的表单,以及文本框本身:

<form id="form1" action="#" method="post">
  <input type="button" id="addRow" value="ADD" />
  <input type="button" id="removeRow" value="REMOVE" onclick="removeRowFromTable(); $('#txtTotalRow').val(tblDetail.rows.length - 1);"/>
  <input type="hidden" name="txtTotalRow" id="txtTotalRow" value="1" />

  <table cellpadding='0' cellspacing='0' border='0' id='tblDetail' width='100%'>
    <tr>
  <th colspan="3">&nbsp;</th>
</tr>
<tr>
  <td>
    <input type="text" name="field1[]" id="ProductID" />
    <a href="#" ><!-- removed - onclick="showProductsModalBox(); return false;" /--> 
      Choose
    </a>
  </td>
</tr>
  </table>
</form>

模态框脚本引用this thread。注入链接的内联javascript将值传递给“field1”文本框,更改为:

<!-- Edited - <a href=\"javascript:;\" onclick=\"$('input[name=field1]').val('".$fetch[0]."');$('#products_modal_box').dialog('close')();\"> /-->
<a href=\"javascript:;\" onclick=\"$('#products_modal_box').data('target').val('".$fetch[0]."');$('#products_modal_box').dialog('close')();\">

该代码用于传递第一个文本框的值。但是,当我尝试添加一个文本框并从模式框中选择一个值时,该值被传递给每个文本框(第一个文本框和第二个文本框),依此类推。如何以不同方式传递每个文本框的值?谢谢。

--- 更新 1 ---

$('#tblDetail tbody').on("click", "a", function() {

    console.log($(this)); // This should be the link you just clicked
    console.log($(this).siblings("input")); // This should be the text box you want to save the value to

    $("#products_modal_box").data("target", $(this).siblings("input"));

    console.log($("#products_modal_box").data("target")); // To confirm it was saved correctly as data

    showProductsModalBox();
    return false;
});

用firebug检查控制台后,结果是:

[a#]
[]
[]

更新 2 - 实际问题

产品 ID 文本框的标记:

<table cellpadding="5px" id="tblDetail" class="tblDetail" width="100%">
  <tr>
    <th colspan="2" width="15%">Product ID</th>
    <th>Problem</th>
  </tr>
  <tr>
    <td><input type="text" name="productid[]" id="productid" size="7"/></td>
    <td><a href="#">Choose</a></td> <!-- THE PROBLEM WAS HERE /-->
    <td><input type="text" name="problem[]" id="problem" /></td>
  </tr>
</table>

<input type="button" id="addRow" value="ADD" />
<input type="button" id="removeRow" value="REMOVE" onclick="removeRowFromTable();"/>

模态框的标记:

<div id="IDBarang_dialog" title="Ambil ID Barang" style="display: none;">
  <div class="in">
    <div class="grid-12-12">
      <form ID="IDBarang_dialog_form" action="#" method="post">
        <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><a href='#' onclick=\"$('#IDBarang_dialog').data('target').val('".$fetch[0]."');$('#IDBarang_dialog').dialog('close')();\">Choose</a></td>"; 
              print "  <td>" . $fetch[0] . "</td>"; //$fetch[0] == Product ID
              print "</tr>";
            }
          ?>
          </tbody>
        </table>
      </form>
    </div>
  </div>
</div>

Javascript:

<script>
  var grid_modal_options = {
    height: 'auto',
    width: '80%',
    modal: true
  };

  function showIDBarangModalPopup() {
    $("#IDBarang_dialog").dialog(grid_modal_options);
    $("#IDBarang_dialog").parent().appendTo('form:first');
  }
</script>  

<script type="text/javascript">
    //MODAL-BOX
    $('#tblDetail tbody').on("click", "a", function() {
      console.log($(this)); 
      console.log($(this).siblings("input")); 
      $("#IDBarang_dialog").data("target", $(this).siblings("input"));

      console.log($("#IDBarang_dialog").data("target")); 

      showIDBarangModalPopup();
      return false;
    });

    //ADD ROW
    $(document).ready(function() {
      $("#addRow").click(function() {
        $('#tblDetail tbody>tr:last').clone(true).insertAfter('#tblDetail tbody>tr:last');

        $('#tblDetail tbody>tr:last #productid').val('');
        $('#tblDetail tbody>tr:last #problem').val(''); 

        return false;
      });
    }); 

    //REMOVE ROW
    function removeRowFromTable() {
      var tbl = document.getElementById('tblDetail');
      var lastRow = tbl.rows.length;
      if (lastRow > 2) tbl.deleteRow(lastRow - 1);
    }

【问题讨论】:

  • 我不知道它是否与您的问题有关,但您的输入定义了nameid 的事实可能会造成麻烦。克隆一个具有 id 的元素后,您应该在将其插入 DOM 之前给克隆一个新的 id,否则可能会发生坏事(虽然不确定是什么)。以你的选择器#tblDetail tbody&gt;tr:last #field1 为例,在你克隆它几次之后,将会有许多id 为#field1 的输入。我建议删除名称和 ID,并改用类:&lt;input type="text" class="myclass" /&gt;

标签: php arrays textbox modal-dialog


【解决方案1】:

要使此工作流程正常工作,您需要做两件事:

  1. 打开模态框时,将选择的目标保存在某处;
  2. 关闭模式框时,将所选值设置为该目标。

对于 (1),我建议首先将 showProductsModalBox()onclick 属性中取出(您无法访问当前元素)并将其移动到 jQuery 处理程序(您可以在其中) .

<a href="#"> <!-- Removed: onclick="showProductsModalBox(); return false;" -->
  Choose
</a>

我正在使用$.on,因此处理程序将适用于当前和未来的行:

$('#tblDetail tbody').on("click", "a", function() {
    $("#products_modal_box").data("target", $(this).siblings("input"));
    showProductsModalBox();
    return false;
});

我建议将目标保存为#products_modal_boxdata 值,以便您可以全局访问它。目标是input 元素,它是您点击的链接thissiblingprev 也可以使用)。

现在对于 (2),您只需检索目标并将选择的值设置为它:

<a href=\"javascript:;\" onclick=\"$('#products_modal_box').data('target').val('".$fetch[0]."');$('#products_modal_box').dialog('close')();\">

就是这样!作为旁注,恕我直言,您应该避免使用 PHP 生成 JavaScript(或为此使用内联 JS),它作为一种快速的“hack”很好,但它并非没有缺点。请参阅链接问题中的qeremy's answer,了解完成相同任务的另一种方法。随着项目的进展,如果您没有清楚地将数据与代码分开,可能会出现越来越多的复杂情况。


更新:我很确定上面的代码是正确的。如果它不适合您,那么您的标记不应该像您描述的那样。在 jsFiddle 检查这个 working example。打印到控制台的值是预期值。

重申,在您的链接上调用siblings 将返回它的同一父级下的元素。如果结构不同,您应该使用其他方式导航到正确的元素。就像parent(上一级)、children(下一级)、closest(上一级)或find(下一级)。如果您需要进一步的帮助,请使用您正在使用的实际标记更新问题。

【讨论】:

  • 您好,感谢您的代码,但它不起作用。该代码用于显示模态框,但不适用于返回从模态框中选择的值。仅供参考,一行中有多个文本框。我省略了上面的内容,因为它只是示例。还是一样吗?
  • 如果链接在文本框的同一个父级下,并且如果(行,列)对中只有一个文本框,那么是的,方法相同。如果同一个td 中有多个文本框,您可以使用类来选择正确的文本框(例如:$(this).siblings(".myclass"))。如果它们在不同的列中,兄弟姐妹不会这样做,请使用.closest("tr").find("input") 选择它们。但是,如果标记是正确的,就像您的示例所示,那么我的代码应该可以正常工作。
  • 还是不行。我检查了控制台并更新了上面的问题。请检查一下..
  • @mrjimoy_05 您确定问题中的 html 与您使用的相同吗?检查我更新的答案和this working example
  • 是的,我也很困惑为什么代码不起作用。我已经更新了上面的代码,类似于我的实际问题,请检查一下。 :)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-08-10
  • 2016-04-16
  • 2023-03-28
  • 1970-01-01
  • 2012-06-01
  • 2016-02-07
  • 2018-04-24
相关资源
最近更新 更多