尽管问题标题给人的印象是我想问一个问题,但目前我面临两个问题。清除输入文本区域与动态输入ID的按钮点击 - jQuery
我已经经历了几个类似的问题,关于如何清除按钮单击上的输入文本区域,但其中大多数是固定输入类或ID的。
问题1:我正在动态生成行,并且所有字段都使用JS填充,因此所有文本框的输入ID都不相同。现在,如果用户在“全部应用”输入栏中输入一个数字,并单击该按钮,则应在所有添加在该投影片上的行中设置相同的数字。
问题2:在betslip输入框中输入各个值后,如果点击“全部清除”按钮,它应该清除投注单上早先输入的所有输入。
下面是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>. ' + betInfo['horseName'] + '</b></p>');
var raceInfo = "";
$("#raceInfo").contents().filter(function() {
if (this.nodeType === 3) raceInfo = $(this).text() + ', ' + 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那里我需要实现那2个功能
function applyToAllBetInput() {
$('.apply').change(function() {
$(this).prevAll().find('input[type=text]').val($(this).val());
});
}
function clearAllBetInput() {
$('.clearall').click(function() {
$('div.bet').find('input').val('');
});
}
我认为这是错误的'var divId = $(this).attr( 'id')。val(),inputId = divId.document.getElementById('input');' – Pavlo
是的,我知道这是错误的。我不能选择输入框与ID'输入'因为没有任何! – zaq
'$(this).find('input')。val('')' - 将会是你的输入 – Pavlo