이들에서 사용자 지정 이름을 할당하여 입력 필드를 동적으로 추가하려고합니다 aa,ab,ac,ad
.
다음은 코드입니다.
<script>
$(function() {
$('body').on('click', '.post-link', function() {
$('#post-container').fadeIn();
var post_title = $(this).closest('div').find('a').text();
var val = $(this).text("val");
if (val == "")
var post_id = "aa";
else if (val == "aa")
var post_id = "ab";
else if (val == "ab")
var post_id = "ac";
else
var post_id = "ad";
if ($('#post-container input').length <= 3)
$('#post-container').append('<input name="' + post_id + '" value="' + post_title + '">-- <a href="#" class="remove-title">REMOVE</a></input>');
});
$('body').on('click', '.remove-title', function() {
$(this).closest('input').remove();
});
});
</script>
HTML :
<button class="post-link" rel="<?php the_ID(); ?>"> ADD </button>
<div id="post-container"> </div>
이제 문제는 이것이 name="ad"
네 필드 모두에 추가된다는 것입니다 . 하지만 이름이 다른 각 필드가 필요합니다.
각 입력 필드에 다른 이름을 지정하는 방법은 무엇입니까?
그리고 제목 제거 링크가 작동하지 않는 이유는 무엇입니까?
다음을 시도하십시오 :
$('body').on('click', '.post-link', function(){
var names = ['aa', 'ab', 'ac', 'ad'];
$('#post-container').fadeIn();
var post_title = $(this).closest('div').find('a').text();
for (var i=0; i<names.length; i++) {
if ($('#post-container').find('input[name=' + names[i] + ']').length < 1) {
$('#post-container').append( '<div class="row"><input name="' + names[i] + '" value="'+post_title+'">-- <a href="#" class="remove-title">REMOVE</a></input></div>' );
break;
}
}
});
$('body').on('click', '.remove-title', function() {
$(this).closest('.row').remove();
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button class="post-link" rel="<?php the_ID(); ?>"> ADD </button>
<div id="post-container"> </div>
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다