我试过这段代码。我的问题是如何将文本的值传递给持有者,每个输入应该用逗号分隔,不应重复输入值,任何人都可以帮助我提前谢谢。
<input type="text" name="text" id="text">
<button type="button" id="button"> Button </button>
<Br> <Br>
<input type="holder" id="holder" name="holder">
<script>
$(document).ready(function() {
$("#button").click(function() {
$('#text').each(function() {
if ($(this).val() == $current.val() && $(this).attr('id') != $current.attr('id'))
{
alert('duplicate found!');
}
var data = [];
$.each($("#text"), function() {
data.push($(this).attr("value"));
});
$('#holder').val(data.join(","));;
});
});
</script>
答案 0 :(得分:0)
请使用以下代码替换您的脚本代码:
<script>
$(document).ready(function() {
var data = [];
$("#button").click(function() {
$.each($("#text"), function() {
if(data.indexOf($("#text").val())<0){
data.push($("#text").val());
$("#text").val('');
}else{
alert('duplicate found');
}
$('#holder').val(data.join(","));
});
});
});
</script>
答案 1 :(得分:0)
试试这个..
$("#button").click(function() {
$text = $('#text');
$textval = $text.val();
$holder = $('#holder');
$holderval = $holder.val();
if ($holderval.trim().length) {
var duplicate;
duplicate = $holderval.split(',').filter(function(hv) {
return hv == $textval;
})
if (duplicate.length) {
alert("Duplicate found");
return false;
}
$holder.val($holderval += ',' + $textval);
} else {
$holder.val($holderval += $textval);
}
$text.val('');
});
$("#button").click(function() {
$text = $('#text');
$textval = $text.val();
$holder = $('#holder');
$holderval = $holder.val();
if ($holderval.trim().length) {
var duplicate;
duplicate = $holderval.split(',').filter(function(hv) {
return hv == $textval;
})
if (duplicate.length) {
alert("Duplicate found");
return false;
}
$holder.val($holderval += ',' + $textval);
} else {
$holder.val($holderval += $textval);
}
$text.val('');
});
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="text" name="text" id="text">
<button type="button" id="button">Button</button>
<Br>
<Br>
<input type="holder" id="holder" name="holder">
&#13;