单击时清除 jQuery 表单字段

发布于 2024-11-30 21:43:45 字数 1404 浏览 1 评论 0原文

我创建了一个表单,其中包含一个用于街道地址的对话框搜索框和一个用于邮政地址的对话框搜索框。它的工作原理是,当您单击搜索结果时,它会根据您单击的按钮填充街道或邮政地址。一切正常,除了当您单击搜索结果时,它会清除其他地址的字段填充。据我所知,这一切都与点击功能有关。任何帮助将不胜感激,谢谢

例如: 如果我单击“邮政地址”按钮并单击搜索结果,它将填充邮政地址字段。然后,如果我单击“街道地址”按钮,它将填充“街道地址”字段,但也会清除“邮政地址”字段。

代码

<!-- STREET ADDRESS -->
<script type="text/javascript">
  $(document).ready(function() {
    $('#table-data tr').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>

<!-- POSTAL ADDRESS -->
<script type="text/javascript">
  $(document).ready(function() {
    $('#table-data tr').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');
    });
  });
</script>

如果您需要,我可以发布更多代码,但我已将其范围缩小到他的点击函数,在此脚本中导致了问题。

谢谢

I created a form with a dialog search box for a Street Address and one for a Postal address. How it works is when you click on the search result it populates either the Street or Postal address depending on which button you clicked. It's all working fine, except when you click on a search result it clears the field population for the other address. As far as I can tell this is all linked to the click function. ANy help will be greatly appreciated, thanks

For example:
If I click on the Postal address button and click a search result it will populate the postal address fields. Then if I click the Street address button, it will populate the Street address fields but also clear the Postal address fields.

Code

<!-- STREET ADDRESS -->
<script type="text/javascript">
  $(document).ready(function() {
    $('#table-data tr').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>

<!-- POSTAL ADDRESS -->
<script type="text/javascript">
  $(document).ready(function() {
    $('#table-data tr').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');
    });
  });
</script>

I can post more code if you need but I've narrowed it down to his click function, in this script that is causing the issue.

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

苏别ゝ 2024-12-07 21:43:45

您将两个事件处理程序(两个 click 处理程序)连接到同一个选择器 (#table-data tr)。因此,当您单击该按钮(看起来像同一个按钮)时,它将触发两个单击处理程序。

我不确定您如何获取数据,但您应该考虑使用两个不同的处理程序和某种标志来确定哪个分支被触发。

$('#table-data tr').click(function () {
  if(do_street_address_work) {
    //your logic for populating the street address
  } else {
    //your logic for populating the postal address
  }
}

You're hooking up two event handlers (two click handlers) to the same selector (#table-data tr). So when you click the button (looks like the same button), it's going to fire both click handlers.

I'm not sure how you're getting your data, but you should consider either having two different handlers and some sort of flag to determine which branch gets fired.

$('#table-data tr').click(function () {
  if(do_street_address_work) {
    //your logic for populating the street address
  } else {
    //your logic for populating the postal address
  }
}
乄_柒ぐ汐 2024-12-07 21:43:45

意识到两个处理程序都是通过单击行来触发的。

如果问题是加载一个地址会清除其他行中设置的地址,则问题在于您的 jquery 选择器正在选择每个字段,而不仅仅是正确的字段。您可以使用此变量将选择器限制为触发单击事件的行:

<!-- STREET ADDRESS -->
<script type="text/javascript">
  $(document).ready(function() {
    $('#table-data tr').click(function () {
      var $row = $(this);

      $('#street_name').val( $row.find('td.address_street').text() );
      $('#suburb').val( $row.find('td.address_suburb').text() );
      $('#city').val( $row.find('td.address_city').text() );
      $("#modal_form").dialog('close');
    });
  });
</script>

如果问题是设置街道地址会清除邮政地址,则问题在于您的单击处理程序位于该行,而不是按钮。您没有发布 HTML,但总体思路是:

<!-- STREET ADDRESS -->
<script type="text/javascript">
  $(document).ready(function() {
    $('#table-data tr .street-button').click(function () {
      var $row = $(this).parents('tr').first();

      $('#street_name').val( $row.find('td.address_street').text() );
      $('#suburb').val( $row.find('td.address_suburb').text() );
      $('#city').val( $row.find('td.address_city').text() );
      $("#modal_form").dialog('close');
    });
  });
</script>

Realize that both handlers are firing with a row click.

If the issue is that loading one address clears the addresses set in the other rows, the problem is that your jquery selector is selecting every field, not just the correct one. You can use the this variable to limit your selectors to the row which fired the click event:

<!-- STREET ADDRESS -->
<script type="text/javascript">
  $(document).ready(function() {
    $('#table-data tr').click(function () {
      var $row = $(this);

      $('#street_name').val( $row.find('td.address_street').text() );
      $('#suburb').val( $row.find('td.address_suburb').text() );
      $('#city').val( $row.find('td.address_city').text() );
      $("#modal_form").dialog('close');
    });
  });
</script>

If the issue is that setting the street address clears the postal address, the problem is that your click handler is on the row, not the button. You didn't post your HTML, but the general idea is:

<!-- STREET ADDRESS -->
<script type="text/javascript">
  $(document).ready(function() {
    $('#table-data tr .street-button').click(function () {
      var $row = $(this).parents('tr').first();

      $('#street_name').val( $row.find('td.address_street').text() );
      $('#suburb').val( $row.find('td.address_suburb').text() );
      $('#city').val( $row.find('td.address_city').text() );
      $("#modal_form").dialog('close');
    });
  });
</script>
如歌彻婉言 2024-12-07 21:43:45

对于“邮政地址”和“街道地址”按钮,我认为这两个事件处理程序都会被调用,主要是您像这样设置两个处理程序:

 $('#table-data tr').click(function ()

所以您需要有比这更具体的选择器。

For both of Postal Address and Street Address buttons, I think both event handler will be called mainly you set both handler like this:

 $('#table-data tr').click(function ()

So you need to have more specific selector than this.

尹雨沫 2024-12-07 21:43:45

谢谢大家,我最终用

$('#table-data tr').live('click',function ()

欢呼解决了这个问题

Thanks guys, I ended up solving it using

$('#table-data tr').live('click',function ()

cheers

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文