单击按钮时清除带有动态输入 ID 的输入文本区域 - jQuery



尽管问题标题给人一种我想问一个问题的印象,但是目前我面临两个问题。

我已经遇到了有关如何在按钮上清除输入文本区域的几个类似问题,但是其中大多数用于固定输入类或ID。

问题1:我正在动态生成行,并且使用JS填充了所有字段,因此所有文本框的输入ID都不同。现在,如果用户在"应用于所有"输入字段上输入一些数字,然后单击按钮,则应将相同的数字设置为BETSLIP中添加的所有行。

问题2:在BETSLIP输入框中输入单个值后,如果我单击"清除所有"按钮。它应该清除BET Slip之前输入的所有输入。

这是HTML结构

<div id="bets">
  <div id="idNo1" class="bet gray2" name="singleBet">
    <div class="left">
      <p class="title">
        <p class="supermid">
          <input id="input_1" type="text">
    </div>
  </div>
  <div id="idNo2" class="bet gray2" name="singleBet">
    <div class="left">
      <p class="title">
        <p class="supermid">
          <input id="input_2" type="text">
    </div>
  </div>
  <div id="idNo3" class="bet gray2" name="singleBet">
    <div class="left">
      <p class="title">
        <p class="supermid">
          <input id="input_3" type="text">
    </div>
  </div>
</div>

js用于在单个下注中添加元素

function createSingleBetDiv(betInfo) {
    var id = betInfo.betType + '_' + betInfo.productId + '_' + betInfo.mpid,
        div = createDiv(id + '_div', 'singleBet', 'bet gray2'),
        a =  createA(null, null, null, 'right orange'),
        leftDiv = createDiv(null, null, 'left'),
        closeDiv = createDiv(null, null, 'icon_shut_bet'),
        singleBetNumber = ++document.getElementsByName('singleBet').length;
    // Info abt the bet
    $(leftDiv).append('<p class="title"><b><span class="bet_no">' + singleBetNumber + '</span>.&nbsp;' + betInfo['horseName'] + '</b></p>');
    var raceInfo = "";
    $("#raceInfo").contents().filter(function () {
        if (this.nodeType === 3) raceInfo = $(this).text() + ',&nbsp;' + betInfo['betTypeName'] + ' (' + betInfo['value'].toFixed(2) + ')';
    });
    $(leftDiv).append('<p class="title">' + raceInfo + '</p>');
    // Closing btn
    (function(id) {
        a.onclick=function() {
            removeSingleBet(id + '_div');
        };
    })(id);
    $(a).append(closeDiv);
    // Creating input field - This is where I am creating the input fields
    $(leftDiv).append('<p class="supermid"><input id="' + id + '_input" type="text" class="betInput"></p>');
    // Creating WIN / PLACE checkbox selection
    $(leftDiv).append('<p><input id="' + id + '_checkbox" type="checkbox"><b>' + winPlace + '</b></p>');
    // Append left part
    $(div).append(leftDiv);
    // Append right part
    $(div).append(a);
    // Appending div with data
    $.data(div, 'mapForBet', betInfo);
    return div;
}

html适用于全部并清除所有按钮

<a href="javascript: applyToAllBetInput()"class="button apply orange">APPLY TO ALL <input type="text"> </a>
<a href="javascript: clearAllBetInput()" class="button clearall gray">CLEAR ALL</a>

JS我需要实现这两个功能

function applyToAllBetInput() {
    $('.apply').change(function() {
        $(this).prevAll().find('input[type=text]').val($(this).val());
    });
}
function clearAllBetInput() {
    $('.clearall').click(function() {
        $('div.bet').find('input').val('');
    });
}

最好的办法是从链接中删除内联事件处理程序,例如...

<a href="#" class="button apply orange">APPLY TO ALL <input type="text"> </a>
<a href="#" class="button clearall gray">CLEAR ALL</a>

然后,在脚本中分配事件处理程序...

$("a.button.apply").on("click", function(e) {
    e.preventDefault();
    applyToAllBetInput($(this).find("input").val());
});
$("a.button.clearall").on("click", function(e) {
    e.preventDefault();
    applyToAllBetInput("");
});

这将应用值适用于所有输入...

function applyToAllBetInput(value) {
    $("#bets div[name=singleBet] .supermid input:text").val(value);
}

如果将参数传递到 applyToAllBetInput中,然后用它设置输入,那么您只需要一个函数,因为它们都做同样的事情,但具有不同的值。最好只有1位代码只做1件事,那么您将来只需要修复一次。

请替换我给出的ID,用您的实际按钮/textarea ID(将ID给出您的元素)。

$('#txtApplyToAll').change(function() {
    $(this).prevAll().find('input[type=text]').val($(this).val());
 });
 $('#btnClearAll').click(function() {
    $('#txtApplyToAll').prevAll().find('input[type=text].val('');
});

甚至在开始编写代码之前,我会提出几个一般建议。首先,当您有可用的jQuery时,为什么要使用Longhand JavaScript?例如:

inputId = divId.document.getElementById('input');

应该简单:

inputId = $(inputId).find('input');

(或类似的东西 - 我不确定您对此的追求。)

接下来,您正在使用内联单击处理程序。而是使用事件听众:

<a href="javascript: applyToAllBetInput()" ...

应该是

$('a#my-id').click(function() { ... }

最后,您可以针对所有输入以使用这样的选择器清除:

$('div.bet').find('input').val('');

最新更新