2013-02-20 3 views
2

나는 사용자의 URL 그래서 이름 입력으로 사용자 유형 john smith, 난 자동으로 URL 입력을 채울 것인지다른 필드를 기반으로 입력 채우기?

에 사용되는 1 개 사용자 이름에 사용되는 필드, 또 다른이 john-smith URL이 끝나기 때문에 /personal-trainer-directory/john-smith

어떻게 자동으로이 작업을 수행합니까? 사용자가 URL 입력란을 작성하지 않아도됩니다.

여기에 예제가 있지만 ExpressionEngine 코드가 있기 때문에 중괄호가있는 이유가 있지만 CMS 대신 코드와 더 관련이 있다고 생각합니다. 여기에 게시하십시오.

내가 현재 가지고있는 것입니다 :

<div class="formsection clearfix"> 
<label for="title">Name</label> 
<span class="directions">The name you want to be displayed in your listing.</span> 
<input type="text" name="title" id="title" value="{title}" size="50" maxlength="100"> 
</div> 

<div class="formsection clearfix"> 
<label for="url_title">URL Title</label> 
<span class="directions">Put a dash between first and last name. example: "john-smith"</span> 
<input type="text" name="url_title" id="url_title" value="{url_title}" maxlength="75" size="50"> 
</div> 

답변

3
$("#title").change(function() { 
    $('#url_title').val('/personal-trainer-directory/'+$(this).val().toLowerCase().replace(/ +/g, '-').trim()); 
}); 

jsFiddle example

+0

감사합니다! 이것은 나에게 가장 가까운 것을 가지고왔다! 나는이 부분이 정확하다는 것을 보여주기를 원한다. $ ('# url_title') .val (''+ $ this) .val(). toLowerCase()./g, '-'). trim());'? –

+0

예,'+ '부분을 제거 할 수 있습니다. – j08691

+0

감사합니다! 위대한 작품 –

1

당신은 같은 일을 할 수있는이

$(function(){ 
    $("#title").blur(function(){ 
     $("#url_title").val('/personal-trainer-directory/' + $(this).val()); 
    }); 
}); 

jsFiddle

또는 당신이 입력으로 채워하려면

$(function(){ 
    $("#title").keyup(function(){ 
     $("#url_title").val('/personal-trainer-directory/' + $(this).val()); 
    }); 

}}; jsFiddle

+0

@RuneFS 이유는 무엇입니까? http://jsfiddle.net/pmatseykanets/6Tzvq/ – peterm

+0

을 참조하십시오. 안경이 필요하기 때문에 : 죄송합니다. 코드를 잘못 읽으 셨습니다. –

관련 문제