【问题标题】:jQuery create multiple instances for already working scriptjQuery为已经工作的脚本创建多个实例
【发布时间】:2017-10-22 16:42:38
【问题描述】:

几天前我问了一个问题,@RustyTheBoyRobot 很好地回答了这个问题。

下面的代码可以 100% 运行,但我需要一种在页面上添加多个“查找地址”按钮的方法。一个用于街道地址,一个用于邮政地址。

谁能告诉我如何在不复制模态框和 Javascript 的情况下做到这一点?

更新:经过一些测试,我弄清楚了我需要做什么,我需要某种方法将变量从按钮传递到表格模式表单,因此当我单击一行时,它只会更新我指定的表单字段。

代码

我在带有表格的模式框中创建了一个表单。当您单击表格行时,它会在父页面上填充一个表单。

模态框

<div id="modal_form" title="Address Search">
<form id="address_search">
    <ul>
        <li><label for="name">Search by street description</label>
            <input type="text" name="street_description" id="street_description" />
            <input type="button" id="search_button" class="form_button" value="Search"></li>
    </ul>
<table width="100%" border="0" cellspacing="0" cellpadding="0" id="table-data">
                  <tbody><tr>
                    <td width="200px"><a href="#">Street</a></td>
                    <td width="200px"><a href="#">Suburb</a></td>
                    <td width="200px"><a href="#">City</a></td>
                  </tr>
                  <tr id="row1">
                    <td class="address_street">Harambee Road</td>
                    <td class="address_suburb">Onerai</td>
                    <td class="address_city">Onerai Rural</td>
                  </tr>
                  <tr id="row2">
                    <td class="address_street">Hutchinson Road</td>
                    <td class="address_suburb">Mt Wellington</td>
                    <td class="address_city">Auckland City</td>
                  </tr>
                  <tr id="row3">
                    <td class="address_street">Kauri Road</td>
                    <td class="address_suburb">Westfordshire</td>
                    <td class="address_city">Palmerston North</td>
                  </tr>
            </tbody></table><!-- /table#table-data -->
        </form>
</div><!-- /div#modal_form -->

Javascript

<script type="text/javascript">
  $(document).ready(function() {
    $('#table-data tr').click(function () {
      var curRowId = $(this).attr("id");
      $('#street_name').val( $('#' + curRowId + ' td.address_street').text() );
      $('#suburb').val( $('#' + curRowId + ' td.address_suburb').text() );
      $('#city').val( $('#' + curRowId + ' td.address_city').text() );
  $("#modal_form").dialog('close');
    });
  });
</script>

父表单,其中填充表单字段

<form id="profile">
<ul>
<li><label for="street_number">Street Number</label><input id="street_number" type="text" placeholder="Street Number" name="street_number" ><input type="button" class="form_button" id="find_address" value="Find Address"></li>
<li><label for="street_name">Street Name</label><input id="street_name" type="text" placeholder="Street Name" name="street_name"  disabled="disabled" ></li>
<li><label for="suburb">Suburb</label><input id="suburb" type="text" placeholder="Suburb" name="suburb"  disabled="disabled" ></li>
<li><label for="city">City</label><input id="city" type="text" placeholder="City" name="city" disabled="disabled" ></li>
<li><input type="submit" id="submit" value="Save"></li>
</ul>
</form>

【问题讨论】:

  • 嗨,我想出了一个更具体的方法来问这个问题,所以我打算把它移到一个新问题上。谢谢

标签: jquery html forms html-table


【解决方案1】:

简而言之,您可以考虑制作一个 jquery 插件。

如果你想出一个统一的方法来用特定的按钮识别地址字段,一个jquery插件可以自动计算出正确的字段等等。

这是一个不错的教程:

http://www.authenticsociety.com/blog/jQueryPluginTutorial_Beginner

【讨论】:

    【解决方案2】:

    我最终使用 jquery 向表 td 添加不同的类名,具体取决于您单击的按钮。

    守则

    <script type="text/javascript">
    $(document).ready(function() {
    $('#find_address').click(function(){
    $('#modal_form').dialog('open');
    $('table#table-data_classy').attr( 'id', 'table-data' );   $('td.address_street_classy').removeClass("address_street_classy").addClass("address_street");    $('td.address_suburb_classy').removeClass("address_suburb_classy").addClass("address_suburb");
    $('td.address_city_classy').removeClass("address_city_classy").addClass("address_city");
    }); //end click handler
    $('#find_address_classy').click(function(){
    $('#modal_form').dialog('open');
    $('table#table-data').attr( 'id', 'table-data_classy' );
    $('td.address_street').removeClass("address_street").addClass("address_street_classy");
    $('td.address_suburb').removeClass("address_suburb").addClass("address_suburb_classy");
    $('td.address_city').removeClass("address_city").addClass("address_city_classy");
    }); //end click handler
    }); //end ready event
    </script>
    
    
    <script type="text/javascript">
      $(document).ready(function() {
        $('#table-data tr').live('click', function () {
          var curRowClass = $(this).attr("class");
          $('#street_name').val( $('.' + curRowClass + ' td.address_street').text() );
          $('#suburb').val( $('.' + curRowClass + ' td.address_suburb').text() );
          $('#city').val( $('.' + curRowClass + ' td.address_city').text() );
          $("#modal_form").dialog('close');
        });
      });
    </script>
    
    <script type="text/javascript">
      $(document).ready(function() {
        $('#table-data_classy tr').live('click',function () {
          var curRowClass = $(this).attr("class");
          $('#street_name_classy').val( $('.' + curRowClass + ' td.address_street_classy').text() );
          $('#suburb_classy').val( $('.' + curRowClass + ' td.address_suburb_classy').text() );
          $('#city_classy').val( $('.' + curRowClass + ' td.address_city_classy').text() );
          $("#modal_form").dialog('close');
        });
      });
    

    干杯

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-03-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-07-23
      相关资源
      最近更新 更多